gpt4 book ai didi

mysql - Mysql select查询中无法使用Case语句?

转载 作者:行者123 更新时间:2023-11-30 01:33:53 24 4
gpt4 key购买 nike

这是我的查询,并按交易类型 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 的余额,然后我需要显示为当前余额账户交易表包括账号、交易类型和金额

最佳答案

您只想要所有贷方/借方金额的总计吗?

如果是这样,可能是以下内容:-

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

关于mysql - Mysql select查询中无法使用Case语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17149871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com