以下是我的查询和按交易类型C分组为Credit,D为借方和
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;
以下是我的结果
CreditBalance DebitBalance
16536 NULL
NULL 9673
现在我要减去A / C的余额然后我需要显示为当前余额 帐户交易表包括帐户号,交易类型和金额
答案 0 :(得分:0)
您按TransactionType
进行分组,因此结果显示在不同的行上。您需要删除group by
并移动条件表达式 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;
答案 1 :(得分:0)
您是否只想要所有信用卡/借记卡金额的总额?
如果可能如下: -
SELECT Accountnumber, SUM(CASE WHEN transactiontype = 'C' THEN amount ELSE -1 * amount END)
FROM accounttransaction
WHERE transactiontype IS NOT NULL
GROUP BY Accountnumber
或者单独提供信用卡和借记金额以及总计
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