首页 文章

where子句在不同年份有两个月

提问于
浏览
2

我想要按年份指定的年份中的选定值 . 季节如下:12月(前一年),1月,2月: Winter ,3月,4月,5月: Spring ,6月,7月,8月: Summer ,9月,10月,11月: Autumn

如果我想按年份分组,我对 Spring ,夏,秋的查询如下:

select avg(value) from table where month(date) between 3 and 5 group by year(date);

但我不知道如何在冬季取得这样的结果,例如,12月是在2017年,1月和2月是在2018年 . 按年份分组非常重要 . 谢谢你的帮助!

1 回答

  • 1

    使用 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);
    

相关问题