我的表格如下
a | b --|-- 0 | 5 1 | 6 2 | 7 3 | 7 4 | 7
我想对群组的'b'求和(a = 0,a = 1,a> = 2)。
示例输出应该类似于
sum | a ----|--- 5 | 0 6 | 1 21 | 2
我应该使用什么查询?
答案 0 :(得分:5)
应该这样做:
SELECT
CASE
WHEN a = 0 THEN '0'
WHEN a = 1 THEN '1'
WHEN a >= 2 THEN '2'
END AS anotherNameThanA,
SUM(b) AS `sum`
FROM yourTable
GROUP BY anotherNameThanA