使用左连接T-SQL进行聚合和重复行

时间:2017-06-30 09:14:48

标签: sql sql-server tsql

根据此SQL script (SQL Fiddle)

,我可以看到以下数据

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Id | Emitter    |   EmitterIBAN                         |  Receiver    |   ReceiverIBAN                           |         Adresss                          |     Value 
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1,   Ernst,         HR53 8827 2118 4692 8207 5,           Kimbra,         CH20 1042 6T0N MDTG JT47 U,                     3256 Arrowood Point         0002,        121.72
2,   Keene,         SK81 1004 7484 7505 6308 9259,        Torrance,       RO23 ZWTR OJKK VAU9 T5P4 2GDY,                  35197 Green Ridge Way,                   82.52
3,   Ernst,         HR53 8827 2118 4692 8207 5,           Kimbra,         CH20 1042 6T0N MDTG JT47 U,                     3256 Arrowood Point         0048,        51.81
4,   Korie,         ME43 9833 9830 7367 4239 60,Roy,      IL69            9686 1536 8102 2219 165,                        5 Swallow Alley,                         88.01
5,   Ernst,         HR53 8827 2118 4692 8207 5,           Kimbra,         CH20 1042 6T0N MDTG JT47 U,                     3256 Arrowood Point         0001,        133.99
6,   Charmine,      BG92 TOXX 8380 785I JKRQ JS,          Sarette,        MU67 RYRU 9293 5875 6859 7111 075X HR,          8 Sage Place,                            36.30
7,   Ernst,         HR53 8827 2118 4692 8207 5,           Kimbra,         CH20 1042 6T0N MDTG JT47 U,                     3256 Arrowood Point         0004,        186.99

我希望得到如下这样的结果

  • 计算一对 EmitterIBAN ReceiverIBAN 制作的操作次数
  • 计算每一对 EmitterIBAN ReceiverIBAN
  • 的总和值

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
NumberOperation | Emitter    |   EmitterIBAN                         |  Receiver    |   ReceiverIBAN                           |         Adresss                          |     SumValue 
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4,                Ernst,         HR53 8827 2118 4692 8207 5,           Kimbra,         CH20 1042 6T0N MDTG JT47 U,                     3256 Arrowood Point           0002,      494,51
1,                Keene,         SK81 1004 7484 7505 6308 9259,        Torrance,       RO23 ZWTR OJKK VAU9 T5P4 2GDY,                  35197 Green Ridge Way,                   82.52
1,                Korie,         ME43 9833 9830 7367 4239 60,Roy,      IL69            9686 1536 8102 2219 165,                        5 Swallow Alley,                         88.01
1,                Charmine,      BG92 TOXX 8380 785I JKRQ JS,          Sarette,        MU67 RYRU 9293 5875 6859 7111 075X HR,          8 Sage Place,                            36.30

我做了一个选择,它给了我一对夫妇的数量和总和值Query Result (SQL Fiddle)


SELECT Count(1)     AS NumberOperation, 
       emitteriban, 
       receiveriban, 
       Sum([value]) AS SumValues 
FROM   tableesperadocetransaction 
GROUP  BY emitteriban, 
          receiveriban 

我需要在结果中添加名称和地址,所以我尝试按照下面的请求进行左连接,我得到了Result (SQL Fiddle)


SELECT * 
FROM   (SELECT Count(1)     AS NumberOperation, 
               emitteriban  AS _EmitterIban, 
               receiveriban AS _ReceiverIban, 
               Sum([value]) AS SumValues 
        FROM   tableesperadocetransaction 
        GROUP  BY emitteriban, 
                  receiveriban) tmp_T 
       LEFT JOIN tableesperadocetransaction 
              ON tableesperadocetransaction.emitteriban = tmp_T._emitteriban 
                 AND tableesperadocetransaction.receiveriban = 
                     tmp_T._receiveriban  

那么我怎样才能纠正我的要求以获得我之前展示的结果。

PS:按姓名,地址分组不会对我的情况有效,因为我的夫妻没有一些地址

2 个答案:

答案 0 :(得分:2)

您只需将Emitter和Address列添加到第一个select语句(以及GROUP BY部分):

select  count(1) as NumberOperation, 
Emitter, 
ReceiverAddress,
EmitterIban, 
ReceiverIban,
SUM([Value]) as SumValues
FROM TableEsperadoceTransaction
Group By EmitterIban,
         ReceiverIban, 
         Emitter,
         ReceiverAddress

答案 1 :(得分:2)

您只需添加DISTINCT

SELECT DISTINCT *
FROM   (SELECT Count(1)     AS NumberOperation, 
               emitteriban  AS _EmitterIban, 
               receiveriban AS _ReceiverIban, 
               Sum([value]) AS SumValues 
        FROM   tableesperadocetransaction 
        GROUP  BY emitteriban, 
                  receiveriban) tmp_T 
       LEFT JOIN tableesperadocetransaction 
              ON tableesperadocetransaction.emitteriban = tmp_T._emitteriban 
                 AND tableesperadocetransaction.receiveriban = 
                     tmp_T._receiveriban