Here is my query and Grouping by Transaction type C for Credit and D for Debit and
SELECT
case
WHEN ACT.TRANSACTIONTYPE='C' THEN SUM(ACT.AMOUNT) END AS CreditBalance,
case
WHEN ACT.TRANSACTIONTYPE='D' THEN SUM(ACT.AMOUNT) END AS DebitBalance
FROM accounttransaction ACT
WHERE ACT.TRANSACTIONTYPE IS NOT NULL
GROUP BY ACT.TRANSACTIONTYPE;
Below is my result
CreditBalance DebitBalance
16536 NULL
NULL 9673
Now I want to Subtract the balance of the A/C then I need to show as Current Balance Account transaction table includes Accountnumber,transactiontype and amount
You are grouping by TransactionType
, so the results show up on different rows. You need to remove the group by
and move the condition expressions inside the sum()
:
select sum(case when ACT.TRANSACTIONTYPE = 'C' then act.amount end) as CreditBalance,
sum(case when ACT.TRANSACTIONTYPE = 'D' then act.amount end) as DebitBalance
from AccountTransaction act
WHERE act.TRANSACTIONTYPE IS NOT NULL;
Do you just want the grand total of all the credit / debit amounts?
If so maybe the following:-
SELECT Accountnumber, SUM(CASE WHEN transactiontype = 'C' THEN amount ELSE -1 * amount END)
FROM accounttransaction
WHERE transactiontype IS NOT NULL
GROUP BY Accountnumber
Or to give the credit and debit amounts separately as well as the overall total
SELECT Accountnumber,
SUM(CASE WHEN transactiontype = 'C' THEN amount ELSE 0 END) AS CreditTotal,
SUM(CASE WHEN transactiontype = 'D' THEN amount ELSE 0 END) AS DebitTotal,
SUM(CASE WHEN transactiontype = 'C' THEN amount WHEN transactiontype = 'D' THEN -1 * amount ELSE 0 END) AS OverallTotal
FROM accounttransaction
WHERE transactiontype IS NOT NULL
GROUP BY Accountnumber
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.