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 實現(xiàn)按天,周,月,季度,年查詢統(tǒng)計數(shù)據(jù)
這里提供了一種方法,挺不錯oracle 實現(xiàn)按周,月,季度,年查詢統(tǒng)計數(shù)據(jù) 。
還在網(wǎng)上看到用trunc來搞也可以,下面是個例子,兩句SQL效果一樣的.
id有重復的,所以group by搞了兩個字段.
只在Oracle數(shù)據(jù)庫里試過,其它庫沒試過。
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
------------------------------------------------------------------------------
//按天統(tǒng)計
select count(dataid) as 每天操作數(shù)量, sum()
from
where
group by trunc(createtime, 'DD'))
//按自然周統(tǒng)計
select to_char(date,'iw'),sum()
from
where
group by to_char(date,'iw')
//按自然月統(tǒng)計
select to_char(date,'mm'),sum()
from
where
group by to_char(date,'mm')
//按季統(tǒng)計
select to_char(date,'q'),sum()
from
where
group by to_char(date,'q')
//按年統(tǒng)計
select to_char(date,'yyyy'),sum()
from
where
group by to_char(date,'yyyy')
總結(jié)
以上所述是小編給大家介紹的oracle實現(xiàn)按天,周,月,季度,年查詢排序方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
您可能感興趣的文章:- oracle 查詢當天數(shù)據(jù)的sql條件寫法
- oracle使用to_date查詢一周的第一天日期
- Oracle查詢最近幾天每小時歸檔日志產(chǎn)生數(shù)量的腳本寫法
- Oracle 獲取上周一到周末日期的查詢sql語句
- Oracle中查詢本月星期5的所有日期列表的語句
- Oracle查詢優(yōu)化日期運算實例詳解
- oracle查詢截至到當前日期月份所在年份的所有月份
- Oracle實現(xiàn)查詢2個日期所跨過的月份列表/日期列表的方法分析
- oracle實現(xiàn)動態(tài)查詢前一天早八點到當天早八點的數(shù)據(jù)功能示例