如何计算SQL中间隔内的值的出现?

时间:2018-04-19 08:12:22

标签: sql sqlite

Source and required table

我有第一张桌子,显示白天城市的降雨量。作为请求,我必须显示下雨的天数,如第二个表格。

关于如何做到这一点的任何想法?

1 个答案:

答案 0 :(得分:1)

SELECT City,
       sum(case when rain = 0               then 1 else 0 end) as '0',
       sum(case when (rain> 0 and rain<= 1) then 1 else 0 end) as ']0-1]',
       sum(case when (rain> 1 and rain<= 2) then 1 else 0 end) as ']1-2]',
       sum(case when (rain> 2)              then 1 else 0 end) as '>2'
FROM MeteoData
group by City;