1

我有一个有趣的要求要实现。能否请你帮忙?
请找到下表

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

我无法通过以下操作来实现它

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
4

3 回答 3

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
于 2012-06-19T14:41:31.273 回答
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 
于 2012-06-19T14:42:08.757 回答
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
于 2012-06-19T14:59:41.483 回答