我想要一个sql,给出接收的数量> = 1024,传输数量为< = 3。如何获得数量大于一定数量的总和
例如,
结果是:
约翰逊列出,因为它已经接收到1112美元以下三个传输自 约翰逊帐户已列出:512美元+ 100美元+ 500美元,泰勒以1美元转让1024美元。威廉姆斯不在那里,因为他在四笔交易中获得1200美元。
我尝试
Select recipient as account_name from transfers group by recipient
having sum(amount)>=1024 and count(amount)<=3
它不能正常工作。 我正在使用PostgreSQL,SQLLites语法也很好。
附件为表和行创建您的方便
create table transfers (
sender varchar(1000) not null,
recipient varchar(1000) not null,
date date not null,
amount integer not null
);
insert into transfers values('Smith','Taylor',convert(date,'2002-09-27'),'1024')
insert into transfers values('Smith','Johnson',convert(date,'2005-06-26'),'512')
insert into transfers values('Williams','Johnson',convert(date,'2010-12-17'),'100')
insert into transfers values('Williams','Johnson',convert(date,'2004-03-22'),'10')
insert into transfers values('Brown','Johnson',convert(date,'2013-03-20'),'500')
insert into transfers values('Johnson','Williams',convert(date,'2007-06-02'),'400')
insert into transfers values('Johnson','Williams',convert(date,'2005-06-26'),'400')
insert into transfers values('Johnson','Williams',convert(date,'2005-06-26'),'200')
你的SQL意味着该人在有3周或更少的交易,而不是表示3或更少的数量超过了1024 –