如何选择金额总和等于一个值并按特定列(发件人或收件人)分组的所有记录?

Posted

技术标签:

【中文标题】如何选择金额总和等于一个值并按特定列(发件人或收件人)分组的所有记录?【英文标题】:How to select all records where sum of the amount is equal a value and is grouped by a specific a column (sender or recipient)? 【发布时间】:2016-07-25 10:30:06 【问题描述】:

源表:

REFERENCE | DATETIME          | AMOUNT  |   SENDER |RECIPIENT   
--------------------------------------------------------------
AAB841    |2016-05-11 14:00:01|200.0000 | SENDER1  |RECIPIENT1
AAB842    |2016-05-11 14:28:05|300.0000 | SENDER2  |RECIPIENT1
AAB868    |2016-05-13 08:15:59|1700.0000| SENDER3  |RECIPIENT1
AAB883    |2016-05-17 13:07:14|1200.0000| SENDER1  |RECIPIENT2
AAB891    |2016-05-30 12:59:16|1200.0000| SENDER2  |RECIPIENT2
AAB892    |2016-05-30 13:10:32|1200.0000| SENDER3  |RECIPIENT2
AAB893    |2016-06-02 10:32:44|1000.0000| SENDER1  |RECIPIENT3
AAB894    |2016-06-02 15:24:58|100.0000 | SENDER2  |RECIPIENT3
AAB895    |2016-06-02 15:33:09|100.0000 | SENDER3  |RECIPIENT3

预期结果: 如果发件人发送超过 2000 条,请选择记录

REFERENCE | DATETIME          | AMOUNT  |  SENDER |RECIPIENT  
------------------------------------------------------------- 
AAB841    |2016-05-11 14:00:01|200.0000 | SENDER1 |RECIPIENT1
AAB883    |2016-05-17 13:07:14|1200.0000| SENDER1 |RECIPIENT2
AAB893    |2016-06-02 10:32:44|1000.0000| SENDER1 |RECIPIENT3
AAB868    |2016-05-13 08:15:59|1700.0000| SENDER3 |RECIPIENT1
AAB892    |2016-05-30 13:10:32|1200.0000| SENDER3 |RECIPIENT2
AAB895    |2016-06-02 15:33:09|100.0000 | SENDER3 |RECIPIENT3 

预期结果:

如果发件人发送少于 2000 条,请选择记录

REFERENCE | DATETIME          | AMOUNT  |  SENDER |RECIPIENT  
-------------------------------------------------------------   
AAB842    |2016-05-11 14:28:05|300.0000 | SENDER2 |RECIPIENT1
AAB891    |2016-05-30 12:59:16|1200.0000| SENDER2 |RECIPIENT2
AAB894    |2016-06-02 15:24:58|100.0000 | SENDER2 |RECIPIENT3

我的初始查询:

select TBL1.REFERENCE, DATE(TBL1.DATETIME), TBL1.AMOUNT, TBL1.DATETIME, TBL1.SENDER, TBL1.RECIPIENT
from SOURCETABLE AS TBL1 
WHERE  (TBL1.DATETIME >= '2015-05-01 00:00:00' and TBL1.DATETIME <= '2016-07-25 23:59:59' )
GROUP BY DATE(TBL1.DATETIME), TBL1.AMOUNT,TBL1.DATETIME,TBL1.SENDER, TBL1.RECIPIENT 
HAVING SUM(TBL1.AMOUNT) > 2000;

--- 0 个结果 ---

但它不起作用。希望有人能告诉我如何解决这个问题。

【问题讨论】:

【参考方案1】:

这是一种方法:

select t.*
from sourcetable t
where sender in (select t2.sender
                 from sourcetable t2
                 where t2.datetime >= '2015-05-01' and t2.datetime <= '2016-07-26'
                 group by t2.sender
                 having sum(t2.amount) > 2000
                ) and 
      t.datetime >= '2015-05-01' and t.datetime <= '2016-07-26';

子查询生成符合条件的发件人列表。然后外部查询选择行。

【讨论】:

【参考方案2】:

替代方法:它将结合两个预期结果

SELECT T1.* FROM <Table> T1 INNER JOIN (
SELECT Sender FROM <table> T GROUP BY Sender HAVING SUM(Amount) > 2000) AS T2 ON T1.Sender = T2.Sender ORDER BY T1.Sender
UNION
SELECT T3.* FROM <Table> T3 INNER JOIN (
SELECT Sender FROM <table> T GROUP BY Sender HAVING SUM(Amount) < 2000) AS T4 ON T3.Sender = T4.Sender ORDER BY T3.Sender

【讨论】:

以上是关于如何选择金额总和等于一个值并按特定列(发件人或收件人)分组的所有记录?的主要内容,如果未能解决你的问题,请参考以下文章

Outlook收件人根据条件键入

如何在 SQL 选择查询中添加“或”?

JavaScript中特定列的显示总和

使用 Socket.io 的通知 - 如何发送给特定的用户/收件人?

如何使用 where 和 Group by 返回多个总和结果

如何按年检索总和(金额)列?