oracle实现按天,周,月,季度,年查询排序方法
程序员文章站
2022-03-14 18:35:20
oracle按天,周,月,季度,年查询排序
天--to_char(t.start_time,'yyyy-mm-dd')
周 --to_char(t.start_time,'...
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') --排序
知识点扩展:oracle 实现按天,周,月,季度,年查询统计数据
这里提供了一种方法,挺不错oracle 实现按周,月,季度,年查询统计数据 。
还在网上看到用trunc来搞也可以,下面是个例子,两句sql效果一样的.
id有重复的,所以group by搞了两个字段.
只在oracle数据库里试过,其它库没试过。
create table consumer_acc ( id varchar2(50) not null , acc_num varchar2(10), datetime date ) select t.id,trunc(t.datetime, 'mm' ) as d, sum (t.acc_num) as n from consumer_acc t --where group by t.id,trunc(t.datetime, 'mm' ) order by n desc ; select t.id,to_char(t.datetime, 'mm' ) d , sum (t.acc_num) n from consumer_acc t --where group by t.id,to_char(t.datetime, 'mm' ) order by n desc ------------------------------------------------------------------------------ //按天统计 select count(dataid) as 每天操作数量, sum() from where group by trunc(createtime, 'dd')) //按自然周统计 select to_char(date,'iw'),sum() from where group by to_char(date,'iw') //按自然月统计 select to_char(date,'mm'),sum() from where group by to_char(date,'mm') //按季统计 select to_char(date,'q'),sum() from where group by to_char(date,'q') //按年统计 select to_char(date,'yyyy'),sum() from where group by to_char(date,'yyyy')
总结
以上所述是小编给大家介绍的oracle实现按天,周,月,季度,年查询排序方法,希望对大家有所帮助