select account
, SUM(balance) as bal
from Main_report
Group BY account
如何将正面和负面记录与上述查询分开? 我想将此查询与下面列出的案例结合起来。
select (case when bal < 0 then bal else 0 end) as debits,
(case when bal > 0 then bal else 0 end) as credits from Main_report
Group BY account
答案 0 :(得分:2)
只需使用您的两个案例并总结其结果:
SELECT account
, SUM(CASE WHEN balance < 0 THEN balance ELSE 0 END) as debits
, SUM(CASE WHEN balance > 0 THEN balance ELSE 0 END) as credits
FROM Main_report
GROUP BY account