查询:
SELECT (CASE
WHEN a.account_number = '123456' THEN 'Savings'
WHEN a.account_number = '123321' THEN 'Credit'
END) AS "Account"
FROM accounts a
GROUP BY (CASE
WHEN a.account_number = '123456' THEN 'Savings'
WHEN a.account_number = '123321' THEN 'Credit'
END);
输出:
+---------+
| Account |
+---------+
| Savings |
| Credit |
+---------+
期望的输出:
+---------+
| Account |
+---------+
| Savings |
| Credit |
| Total |
+---------+
鉴于此查询,如何在底部添加总计新行? 后来我要做更多交易......
答案 0 :(得分:3)
大概是储蓄==借记?
在编程语言而不是查询中执行此操作可能更容易,但您可以尝试以下方法:
SELECT IFNULL((CASE
WHEN a.account_number = '123456' THEN 'Savings'
WHEN a.account_number = '123321' THEN 'Credit'
END), "Total") AS "Account"
FROM accounts a
GROUP BY a.account_number WITH ROLLUP;
我还没试过这个,所以不是100%肯定它会起作用,或者如何命名它" Total" (它将在NULL
下)。
请参阅WITH ROLLUP。
答案 1 :(得分:0)
查看你的图表是你要追求的总排吗?我有点困惑。
但是,如果它是将所有信用和借记加在一起的总行,那么我会查看UNION声明。
基本示例:
SELECT a.AccountCreditsDebits From Table
UNION
SELECT SUM(a.AccountCreditsDebits) From Table