不同年份有两个月的 where 子句
where clause with two months in different years
我想要 select 一年中指定季节的值按年份分组。季节如下:
十二月(前一年)、一月、二月:冬季,
三月、四月、五月:Spring,
六月、七月、八月:夏季,
九月、十月、十一月:秋季
如果我想获得按年分组的单季,我对 Spring、夏季和秋季的查询如下所示:
select avg(value) from table where month(date) between 3 and 5 group by year(date);
但是我不知道如何在冬天实现这样的结果,例如,2017 年是 12 月,2018 年是 1 月和 2 月。
按年份分组非常重要。
谢谢你的帮助!
使用in
:
select year(date), avg(value)
from table
where month(date) in (12, 1, 2)
group by year(date);
如果您希望月份为 "contiguous",以便十二月、一月和二月在同一年,则在 group by
:
上加一个月
select year(date + interval 1 month), avg(value)
from table
where month(date) in (12, 1, 2)
group by year(date + interval 1 month);
我想要 select 一年中指定季节的值按年份分组。季节如下: 十二月(前一年)、一月、二月:冬季, 三月、四月、五月:Spring, 六月、七月、八月:夏季, 九月、十月、十一月:秋季
如果我想获得按年分组的单季,我对 Spring、夏季和秋季的查询如下所示:
select avg(value) from table where month(date) between 3 and 5 group by year(date);
但是我不知道如何在冬天实现这样的结果,例如,2017 年是 12 月,2018 年是 1 月和 2 月。 按年份分组非常重要。 谢谢你的帮助!
使用in
:
select year(date), avg(value)
from table
where month(date) in (12, 1, 2)
group by year(date);
如果您希望月份为 "contiguous",以便十二月、一月和二月在同一年,则在 group by
:
select year(date + interval 1 month), avg(value)
from table
where month(date) in (12, 1, 2)
group by year(date + interval 1 month);