已知 ,数据库两个字段,年,月。现在要按照季度分组查询,怎么写sql语句?
发布网友
发布时间:2022-06-06 15:22
我来回答
共1个回答
热心网友
时间:2023-10-10 09:15
先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