select country,sum(severity) as s0 from table group by country;
select country,count(1) as s1 from table where severity>40 group by country;
select country,count(1) as s2 from table where severity>80 group by country;
3次查出的 s0,s1,s2 可以放在1个返回表里么? 就是这样 country,s0,s1,s2 的结果。(要考虑性能)
------解决方案--------------------
SELECT
country,
SUM(severity) AS s0,
COUNT(CASE WHEN severity > 40 THEN 1 END) AS s1,
COUNT(CASE WHEN severity > 80 THEN 1 END) AS s2
GROUP BY country
------解决方案--------------------
select country,sum(severity) as s0,
SUM(CASE WHEN severity>40 THEN 1 ELSE 0 END) as s1,
SUM(CASE WHEN severity>80 THEN 1 ELSE 0 END) as s2
from table
group by country
------解决方案--------------------
select country,SUM(case when severity>80 then 1 else 0 end) as s2,
SUM(case when severity>40 and severity<=80 then 1 else 0 end) as s1,
sum(severity) as s0
from tb
group by country