2017-06-30 55 views
3

我有,你可以看到波紋管在此基礎上SQL script (SQL Fiddle)聚集和重複線與左連接T-SQL


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 
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 

的數據,我想獲得的結果就像下面這一個

  • 計算操作,一對夫婦的EmitterIBANReceiverIBAN製成
  • 計算每個coupl總和值的數量的ËEmitterIBANReceiverIBAN

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
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

您可以再補充發射器和地址欄的第一個SELECT語句(以及GROUP BY部分):

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

如果你只需要一個地址,每個組再一個簡單的解決將是SELECT MAX(地址),而不是GROUP BY地址 – user5480949

+0

是的,它的工作感謝幫助 – Esperadoce

+0

我有145萬條記錄,你可以請告訴我什麼是最好的解決方案是你的還是@Tien Nguyen給出的 – Esperadoce

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