您好我试图以横向方式显示COUNT个结果,但没有成功。有没有简单的方法呢?
我有这个表(在excel中创建用于演示目的)
现在我想显示这样的结果。
任何建议都将不胜感激。
答案 0 :(得分:5)
SELECT store_name,
SUM(CASE WHEN status = 'hold' THEN 1 ELSE 0 END) AS hold_count,
SUM(CASE WHEN status = 'ship' THEN 1 ELSE 0 END) AS ship_count,
SUM(CASE WHEN status = 'return' THEN 1 ELSE 0 END) AS return_count
FROM table
group by store_name
答案 1 :(得分:2)
在sum-aggregate中使用if子句。
select store_name,
sum(if(status='hold', 1, 0)) as 'hold',
sum(if(status='ship', 1, 0)) as 'ship',
sum(if(status='return', 1, 0)) as 'return'
from mytable
group by store_name;