select to_char(sysdate,'q') from sys.dual;
SqlServer 季节统计
create table tb(date datetime, value int)
insert into tb values(getdate() - 365 , 0)
insert into tb values(getdate() - 365 , 1)
insert into tb values(getdate() - 365 , 2)
insert into tb values(getdate() - 365 , 3)
insert into tb values(getdate() - 365 , 4)
insert into tb values(getdate() - 265 , 1)
insert into tb values(getdate() - 165 , 2)
insert into tb values(getdate() - 165 , 3)
insert into tb values(getdate() - 100 , 0)
insert into tb values(getdate() - 10 , 2)
insert into tb values(getdate() - 1 , 4)
insert into tb values(getdate() - 1 , 54)
insert into tb values(getdate() - 20 , 3)
go
select datepart(year,date) [年],
sum(case datepart(quarter,date) when 1 then value else 0 end ) [第一季度],
sum(case datepart(quarter,date) when 2 then value else 0 end ) [第二季度],
sum(case datepart(quarter,date) when 3 then value else 0 end ) [第三季度],
sum(case datepart(quarter,date) when 4 then value else 0 end ) [第四季度],
sum(value) 全年
from tb
group by datepart(year,date)
drop table tb