0

这是我的查询和分组,按交易类型 C 表示贷方,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 的余额,然后我需要显示为 Current Balance Account 交易表包括 Accountnumber、transactiontype 和 amount

4

2 回答 2

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;
于 2013-06-17T14:24:36.313 回答
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
于 2013-06-17T15:16:27.137 回答