Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL:-我正在尝试获取表中的行,其中最多3行的金额应小于等于1024_Sql_Postgresql - Fatal编程技术网

SQL:-我正在尝试获取表中的行,其中最多3行的金额应小于等于1024

SQL:-我正在尝试获取表中的行,其中最多3行的金额应小于等于1024,sql,postgresql,Sql,Postgresql,这是我的桌子 sender | recipient | date | amount ------------+------------+------------+-------- Smith | Williams | 2000-01-01 | 200 Smith | Taylor | 2002-09-27 | 1024 Smith | Johnson | 2005-06-26 | 512 Wi

这是我的桌子

   sender     | recipient  | date       | amount
  ------------+------------+------------+--------
   Smith      | Williams   | 2000-01-01 | 200
   Smith      | Taylor     | 2002-09-27 | 1024
   Smith      | Johnson    | 2005-06-26 | 512
   Williams   | Johnson    | 2010-12-17 | 100
   Williams   | Johnson    | 2004-03-22 | 10
   Brown      | Johnson    | 2013-03-20 | 500
   Johnson    | Williams   | 2007-06-02 | 400
   Johnson    | Williams   | 2005-06-26 | 400
   Johnson    | Williams   | 2005-06-26 | 200
查询应该只返回taylor和Johnson

因为泰勒在一行中有1024行,而约翰逊在三行中有512、100、500=1112行,但威廉姆斯没有,因为超过1024行需要四行

我尝试了以下查询:

select   
    q1.r, q1.sum1, q1.c 
from
    (select  
         recipient as r, count(*) as c, sum(amount) as sum1
     from 
         transfers 
     group by 
         recipient) as q1
where 
    c <= 3 AND sum1 <= 1024

您可以获得每个收件人的前3个金额,然后使用SUM和HAVING:


您可以获得每个收件人的前3个金额,然后使用SUM和HAVING:


您可以在窗口函数和聚合中执行此操作

Select recipient
From (select t.*, row_number() over (partition by recipient order by amount desc) as seqnum
      From t
     ) t
Where seqnum <= 3
Group by recipient
Having sum(amount) > 1024

您可以在窗口函数和聚合中执行此操作

Select recipient
From (select t.*, row_number() over (partition by recipient order by amount desc) as seqnum
      From t
     ) t
Where seqnum <= 3
Group by recipient
Having sum(amount) > 1024

这些列与您的列不完全匹配,但这应该可以满足您的需要

select * from (
select name, 
sum(amount) as total, 
count(*) as rec_count
from transfers 
group by name 
having count(*) <= 3) tbl
where total >= 1024

这些列与您的列不完全匹配,但这应该可以满足您的需要

select * from (
select name, 
sum(amount) as total, 
count(*) as rec_count
from transfers 
group by name 
having count(*) <= 3) tbl
where total >= 1024

使用Postgresql的横向连接功能:

select a.recipient,sum(c.amount) totamount from
    (select distinct recipient from testtable ) a 
  left join lateral 
    (select amount from testtable where recipient=a.recipient 
     order by amount desc limit 3) c 
  on true group by a.recipient having sum(c.amount) >= 1024 ;

使用Postgresql的横向连接功能:

select a.recipient,sum(c.amount) totamount from
    (select distinct recipient from testtable ) a 
  left join lateral 
    (select amount from testtable where recipient=a.recipient 
     order by amount desc limit 3) c 
  on true group by a.recipient having sum(c.amount) >= 1024 ;

这是用于哪个RDBMS的?请添加一个标记,以指定您使用的是mysql、postgresql、sql server、oracle还是db2,或者其他完全不同的东西。对不起,我只是从查询开始。postgresqlTitle的意思是您希望>1024,但您的sql表示希望>1024,很抱歉,最大行数是3,最小行数是3?c这是用于哪个RDBMS的?请添加一个标记,以指定您使用的是mysql、postgresql、sql server、oracle还是db2,或者其他完全不同的东西。对不起,我只是从查询开始。postgresqlTitle的意思是您希望>1024,但您的sql表示希望>1024,很抱歉,最大行数是3,最小行数是3?c抱歉,我刚刚更新,总和必须小于1024,尽管我给了@Ashvitha,但它似乎仍然没有获得正确的输出。您当前的查询似乎很好?有什么问题吗?第1行:有“Taylor”,“1024”,“1”,预期的“Johnson”有“Johnson”和“taylorSorry”我刚刚更新,总和必须小于1024,而且似乎仍然没有得到正确的输出,尽管我给了@Ashvitha你当前的查询似乎很好?有什么问题吗?第1行:有“泰勒”,“1024”,“1”,预期是“约翰逊”,包括约翰逊和泰勒