我的桌子叫天气:
snowfall dayoftheyear observationdate
0.1 28 2019-01-28
1.9 67 2019-03-08
0.13 316 2019-11-12我一共有137行,随机日最大为365,最大观测日期为2019年-12-31。
我的目标是了解2019年至2020年期间每个月降雪的天数。这意味着,如果一月发生了12次observation_date,那么12就是答案。如何做到这一点?我试过-
select
sum(snowfall ) precipitation,
right(observation_date, 7) EachMonth
from weather
where observation_date between '2019-01-01' and '2019-12-31' and city = 'Olympia' and snowfall > 0
group by EachMonth
order by EachMonth 我每次约会都会收到总数,比如-
snowfall EachMonth
0.24 9-01-04但是我不知道每个月下雪的总天数。期望结果
TotalDaysofSnowfall EachMonth
4 01
7 08发布于 2022-11-17 02:01:11
正如戴尔提到的,使用count(*)。而且您不能在GROUP BY中使用列别名
select
count(*) as TotalDaysofSnowfall,
datepart(month, observation_date) as EachMonth
from
weather
where
observation_date between '2019-01-01' and '2019-12-31'
and city = 'Olympia'
and snowfall > 0
group by
datepart(month, observation_date)
order by
EachMonth https://stackoverflow.com/questions/74468992
复制相似问题