更新时间:2023-02-27 09:28:01
将 GROUP 移动到具有更多列的条件 SUM 中?
Move the GROUP into a conditional SUM with more columns?
select
sum(CASE WHEN fcompany = 'Acme'
THEN fann_sales ELSE 0 END) AS redsales,
sum(CASE WHEN fcompany = 'Acme Rockets'
THEN fann_sales ELSE 0 END) AS bluesales
sum(CASE WHEN fcompany NOT IN ('Acme Rockets', 'Acme')
THEN fann_sales ELSE 0 END) AS greensales
FROM
slcdpm
为此,请跳过桌子.UNION ALL 或子查询方法(在其他答案中)将在每个子句中触及表一次 = 稍慢.
One pass over the table for this. A UNION ALL or subquery approach (in other answers) will touch the table once per clause = somewhat slower.