I have one intresting requirement to achieve. could you please help?
Please find the below tables
**CheckingAccountTable (Assume Table Name – 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 (Assume Table Name – 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
RESULT SHOULD LOOKS LIKE
AccountNo Count(TransactionId) Sum(Balance1+Balance2)
123 6 107
124 4 17
Ok, Here, CAT is the main table and SAT is the sub table to return the balance
I want to find out count of transactionID’s and Sum(Balance1 + Balance 2)
Conditions. If the Balance1 OR Balance 2 is 0, I can’t take transactionID into count
Am not able to acheive it by doing below
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
Try the following: