oracle按天,周,月,季度,年查询排序

时间:2022-02-12 04:59:54

oracle按天,周,月,季度,年查询排序

天--to_char(t.start_time,‘YYYY-MM-DD‘)
周 --to_char(t.start_time,‘YYYY‘),to_char(t.start_time,‘IW‘)
月度--to_char(t.start_time,‘YYYY-MM‘)
季度--to_char(t.start_time,‘YYYY‘),to_char(t.start_time,‘Q‘)
年度--to_char(t.start_time,‘YYYY‘)

按天查询

select to_char(t.start_time,‘YYYY-MM-DD‘) day ,count(*) from test t
where to_char(t.start_time,‘YYYY‘)=‘2019‘ --条件限制
group by to_char(t.start_time,‘YYYY-MM-DD‘) --分组
order by to_char(t.start_time,‘YYYY-MM-DD‘) --排序

按周查询

select to_char(t.start_time,‘YYYY‘) year ,to_char(t.start_time,‘IW‘),count(*) from test t
where to_char(t.start_time,‘YYYY‘)=‘2019‘ --条件限制
group by to_char(t.start_time,‘YYYY‘) year ,to_char(t.start_time,‘IW‘)--分组
order by to_char(t.start_time,‘YYYY‘) year,to_char(t.start_time,‘IW‘) --排序

按月度查询

select to_char(t.start_time,‘YYYY-MM‘) ,count(*) from test t
where to_char(t.start_time,‘YYYY‘)=‘2019‘ --条件限制
group by to_char(t.start_time,‘YYYY-MM‘) --分组
order byto_char(t.start_time,‘YYYY-MM‘) --排序

按季度查询

select to_char(t.start_time,‘YYYY‘) year ,to_char(t.start_time,‘Q‘),count(*) from test t
where to_char(t.start_time,‘YYYY‘)=‘2019‘ --条件限制
group by to_char(t.start_time,‘YYYY‘) ,to_char(t.start_time,‘Q‘)--分组
order byto_char(t.start_time,‘YYYY‘) ,to_char(t.start_time,‘Q‘)--排序

按年度查询

select to_char(t.start_time,‘YYYY‘) year ,count(*) from test t
where to_char(t.start_time,‘YYYY‘)=‘2019‘ --条件限制
group by to_char(t.start_time,‘YYYY‘) --分组
order by to_char(t.start_time,‘YYYY‘) --排序