加入并汇总多列

时间:2015-08-29 11:55:30

标签: sql-server database tsql

表TB1:

| PaymentID | CashAmount |
--------------------------
|     P1    |       3,000|
|     P2    |       5,000|
|     P3    |       8,000|

表TB2:

|ChequeID| PaymentID |ChequeAmount|
-----------------------------------
|   C1   |     P2    |      10,000|
|   C2   |     P1    |      15,000|
|   C3   |     P1    |       2,000|

表TB3:

|TransferID| PaymentID |TransferAmount|
---------------------------------------
|    T1    |     P2    |        20,000|
|    T2    |     P2    |        20,000|
|    T3    |     P1    |         3,000|

预期结果

| PaymentID | CashAmount |ChequeAmount|TransferAmount|
------------------------------------------------------
|     P1    |       3,000|      17,000|         3,000|
|     P2    |       5,000|      10,000|        40,000|
|     P3    |       8,000|        NULL|          NULL|

如何编写查询JOIN并将所有这些表汇总到一起? 我尝试编写简单的连接查询,但结果是错误的。

错误查询的示例:

SELECT    
    TB1.PaymentID, TB1.CashAmount,
    SUM(TB2.ChequeAmount) AS ChequeAmount, 
    SUM(TB3.TransferAmount) AS TransferAmount
FROM        
    TB1
LEFT JOIN 
   TB2 ON TB1.PaymentID = TB2.PaymentID
LEFT JOIN 
   TB3 ON TB1.PaymentID = TB3.PaymentID
GROUP BY    
   TB1.PaymentID, TB1.CashAmount

结果不正确:

| PaymentID | CashAmount | ChequeAmount | TransferAmount |
----------------------------------------------------------
|     P1    |      3,000 |      17,000  |         6,000  |
|     P2    |      5,000 |      20,000  |        40,000  |
|     P3    |      8,000 |        NULL  |          NULL  |

3 个答案:

答案 0 :(得分:3)

您遇到的问题是您正在加入具有相同ID的多行的表,并且它会将这些数字相乘。您需要先完成总和,然后加入数据。假设任何表中都可能缺少数据,您还需要使用完全外连接。如果TB1总是有行,那么左外连接就足够了。

这是你可以做到的:

SELECT      
  coalesce(TB1.PaymentID,TB2.PaymentID,TB3.PaymentID), 
  TB1.CashAmount, TB2.ChequeAmount, TB3.TransferAmount
from (
  select PaymentID, SUM(CashAmount) AS CashAmount
  from TB1 group by PaymentID
) TB1
full outer join (
  select PaymentID, SUM(ChequeAmount) AS ChequeAmount
  from TB2 group by PaymentID
) TB2 on TB1.PaymentID = TB2.PaymentID
full outer join (
  select PaymentID, SUM(TransferAmount) AS TransferAmount
  from TB3 group by PaymentID
) TB3 on isnull(TB1.PaymentID, TB2.PaymentID) = TB3.PaymentID

SQL Fiddle

中的示例

答案 1 :(得分:1)

我会先消除次要表中的重复项,然后(左)加入并与主表相加,就像这样

SELECT      
  TB1.PaymentID, 
  TB1.CashAmount, 
  Sum(TB2.ChequeAmount) as ChequeAmount, 
  Sum(TB3.TransferAmount) as TranAmt
from TB1 
left join (
  select PaymentID, SUM(ChequeAmount) AS ChequeAmount
  from TB2 group by PaymentID
) TB2 on TB1.PaymentID = TB2.PaymentID
left join (
  select PaymentID, SUM(TB3.TransferAmount) AS TransferAmount
  from TB3 group by PaymentID
) TB3 on isnull(TB1.PaymentID, TB2.PaymentID) = TB3.PaymentID
group by
tb1.PaymentId, tb1.CashAmount

答案 2 :(得分:0)

WITH  t2
        AS (
             SELECT [PaymentId], SUM([ChequeAmount]) AS ChequeAmount
             FROM   TB2
             GROUP BY [PaymentId]
           ),
      t3
        AS (
             SELECT [PaymentId], SUM([TransferAmount]) AS TransferAmount
             FROM   TB3
             GROUP BY [PaymentId]
           )
  SELECT  t.PaymentId, SUM(CashAmount) AS CashAmount,
          SUM(t2.[ChequeAmount]) AS [ChequeAmount],
          SUM(t3.[TransferAmount]) AS [TransferAmount]
  FROM    TB1 AS [t]
  LEFT JOIN [t2] ON [t2].[PaymentId] = [t].[PaymentId]
  LEFT JOIN [t3] ON [t3].[PaymentId] = [t].[PaymentId]
  GROUP BY [t].[PaymentId];