已知 ,数据库两个字段,年,月。现在要按照季度分组查询,怎么写sql语句?
1个回答
展开全部
先case when将月份变成季度,然后再group by即可,SQL如下:
select year, quarter, count(1)
from (
select year,
case
when month < 4 then 1
when month < 7 then 2
when month < 10 then 3
else 4
end as quarter
from table
) as result
group by year, quarter
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询