用于加入具有某些业务条件的两个表的SQL

时间:2012-06-19 14:35:21

标签: sql

我有一个有趣的要求来实现。你能帮忙吗? 请查看下表

* * CheckingAccountTable(假设表名 - CAT)

AccountNo  TransactionId    BalanceID       Balance1
123           080              103             $30
123           081              103              $20
123           082              103              $10
123           083              62              $0
123           084              63              $0  
123           085              64              $0
123           086              65              $0
123           087              66              $0  
124           090              103             $3
124           091              103             $2
124           092              103             $1
124           093              72              $0
124           094              73              $0  
124           095              74              $0
124           096              75              $0
124           097              76              $0

SavingsAccountTable(假设表名 - SAT)

BalanceID    Balance2
103                    $0
62                    $15
63                    $12  
64                    $20
65                    $0
66                    $0
72                    $11
73                    $0  
74                    $0
75                    $0
76                    $0

结果应该看起来像

AccountNo  Count(TransactionId)  Sum(Balance1+Balance2)
123            6                   107
124            4                   17  

好的,这里,CAT是主表,SAT是返回余额的子表 我想找出transactionID和Sum(Balance1 + Balance 2)的数量

条件。如果Balance1 OR Balance 2为0,我不能将transactionID计入count

通过以下操作无法实现

SELECT  T.AccountNo,Count(T.TransactionId),
                SUM(T.Balance1 + T.Balance2) 'Stressdays'
        FROM 
                (
                    SELECT  CAT.AccountNo,CAT.Balance1,CAT.BalanceID,CAT,TransactionId  
                            ,CASE   WHEN SAT.BalanceID= 103 THEN
                                        0
                                    ELSE
                                        SAT.Balance2
                                    END AS 'Balance2'
                    FROM    CAT
                            INNER   JOIN  SAT ON CAT.BalanceID = SAT.BalanceID
                            WHERE   CAT.Balance1 <> 0
                            AND     SAT.Balance2 <> 0
                )T
        GROUP BY T.AccountNo

3 个答案:

答案 0 :(得分:3)

如果我了解您的需求,请尝试以下方法:

SELECT cat.AccountNo, Count(TransactionId), SUM(cat.Balance1+sat.Balance2)
FROM cat INNER JOIN sat 
    ON cat.BalanceID = sat.BalanceID
   AND (cat.Balance1 <> 0 OR sat.Balance2 <> 0)
GROUP BY cat.AccountNo

答案 1 :(得分:1)

尝试以下方法:

SELECT  CAT.AccountNo, 
        SUM(CASE WHEN CAT.Balance1 <> 0 OR ISNULL(SAT.Balance2,0) <> 0 THEN 1 ELSE 0 END) CountTransactionId,
        SUM(CASE WHEN CAT.Balance1 <> 0 OR ISNULL(SAT.Balance2,0) <> 0 THEN CAT.Balance1+SAT.Balance2 ELSE 0 END) SumBalance1Balance2
FROM CAT
LEFT JOIN SAT
ON CAT.BalanceId = SAT.BalanceId 

答案 2 :(得分:1)

SELECT C.AccountNo,Count(C.TransactionId) AS [Count(TransactionId)],
     SUM(C.Balance1 + S.Balance2) AS [Sum(Balance1+Balance2)]
FROM SO_CAT C
JOIN SO_SAT S ON S.BalanceID =  C.BalanceID--  AND S.BAlance2 <> 0
WHERE C.BALANCE1 <> 0 OR S.BALANCE2 <> 0
GROUP BY C.ACCOUNTNO