您好,登錄后才能下訂單哦!
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實現按天,周,月,季度,年查詢排序方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。