Oracle按日期統計
阿新 • • 發佈:2019-05-11
sele ear 建表 desc ike day 裏的 wmi acl
--創建表 Test
CTEATE TABLE TEST(ID NUMBER NOT NULL,MODIFIEDTIME DATE NOT NULL)
--按天統計 SELECT TO_CHAR(T.MODIFIEDTIME,'YYYY-MM-DD') TIME,COUNT(*) COUNT FROM TEST T --這裏可加查詢條件 WHERE TO_CHAR(T.MODIFIEDTIME,'YYYY') = TO_CHAR(SYSDATE,'YYYY') GROUP BY TO_CHAR(T.MODIFIEDTIME,'YYYY-MM-DD') --根據日期來分組 ORDER BY TO_CHAR(T.MODIFIEDTIME,'YYYY-MM-DD') ASC NULLS LAST--根據日期排序
--按周統計 SELECT TO_CHAR(T.MODIFIEDTIME,'YYYY') YEAR,TO_CHAR(T.MODIFIEDTIME,'IW') TIME,COUNT(*) COUNT FROM TEST T --這裏可加查詢條件 WHERE TO_CHAR(T.MODIFIEDTIME,'YYYY') = TO_CHAR(SYSDATE,'YYYY') GROUP BY TO_CHAR(T.MODIFIEDTIME,'IW'),TO_CHAR(T.MODIFIEDTIME,'YYYY') --根據周數來分組 ORDER BY TO_CHAR(T.MODIFIEDTIME,'YYYY'),TO_CHAR(T.MODIFIEDTIME,'IW') ASC NULLS LAST --根據周數來排序
--按月統計 SELECT TO_CHAR(T.MODIFIEDTIME,'YYYY-MM') TIME,COUNT(*) COUNT FROM TEST T --這裏可加查詢條件 WHERE TO_CHAR(T.MODIFIEDTIME,'YYYY') = TO_CHAR(SYSDATE,'YYYY') GROUP BY TO_CHAR(T.MODIFIEDTIME,'YYYY-MM') --根據月份來分組 ORDER BY TO_CHAR(T.MODIFIEDTIME,'YYYY-MM') ASC NULLS LAST --根據月份來排序
--按季度統計
SELECT TO_CHAR(T.MODIFIEDTIME,'YYYY') YEAR,TO_CHAR(T.MODIFIEDTIME,'Q') TIME,COUNT(*) COUNT
FROM TEST T
--這裏可加查詢條件 WHERE TO_CHAR(T.MODIFIEDTIME,'YYYY') = TO_CHAR(SYSDATE,'YYYY')
GROUP BY TO_CHAR(T.MODIFIEDTIME,'Q'),TO_CHAR(T.MODIFIEDTIME,'YYYY') --根據每年季度來分組
ORDER BY TO_CHAR(T.MODIFIEDTIME,'YYYY'),TO_CHAR(T.MODIFIEDTIME,'Q') ASC NULLS LAST --根據每年季度來排序
--按年統計
SELECT TO_CHAR(T.MODIFIEDTIME,'YYYY') YEAR,COUNT(*) COUNT
FROM TEST T
--這裏可加查詢條件
GROUP BY TO_CHAR(T.MODIFIEDTIME,'YYYY') --根據年份來分組
ORDER BY TO_CHAR(T.MODIFIEDTIME,'YYYY') ASC NULLS LAST--根據年份來排序
註:MODIFIEDTIME 為 表TEST裏的時間字段,時間類型
以上代碼可直接在數據庫裏運行
假如表裏還有個數量的字段,要按天統計數量,可將COUNT(*)改為SUM(1)函數
我們很容易的就能發現按照時間統計也是有規律的...
--按天統計
SELECT TO_CHAR(T.MODIFIEDTIME,'YYYY-MM-DD') TIME,COUNT(*)COUNT
FROM TEST T
--這裏可加查詢條件 WHERE TO_CHAR(T.MODIFIEDTIME,'YYYY') = TO_CHAR(SYSDATE,'YYYY')
GROUP BY TO_CHAR(T.MODIFIEDTIME,'YYYY-MM-DD') --根據日期來分組
ORDER BY TO_CHAR(T.MODIFIEDTIME,'YYYY-MM-DD') ASC NULLS LAST --根據日期排序
--按周統計
SELECT TO_CHAR(T.MODIFIEDTIME,'YYYY') YEAR,TO_CHAR(T.MODIFIEDTIME,'IW') TIME,COUNT(*) COUNT
FROM TEST T
--這裏可加查詢條件 WHERE TO_CHAR(T.MODIFIEDTIME,'YYYY') = TO_CHAR(SYSDATE,'YYYY')
GROUP BY TO_CHAR(T.MODIFIEDTIME,'IW'),TO_CHAR(T.MODIFIEDTIME,'YYYY') --根據周數來分組
ORDER BY TO_CHAR(T.MODIFIEDTIME,'YYYY'),TO_CHAR(T.MODIFIEDTIME,'IW') ASC NULLS LAST --根據周數來排序
--按月統計
SELECT TO_CHAR(T.MODIFIEDTIME,'YYYY-MM') TIME,COUNT(*) COUNT
FROM TEST T
--這裏可加查詢條件 WHERE TO_CHAR(T.MODIFIEDTIME,'YYYY') = TO_CHAR(SYSDATE,'YYYY')
GROUP BY TO_CHAR(T.MODIFIEDTIME,'YYYY-MM') --根據月份來分組
ORDER BY TO_CHAR(T.MODIFIEDTIME,'YYYY-MM') ASC NULLS LAST --根據月份來排序
--按季度統計
SELECT TO_CHAR(T.MODIFIEDTIME,'YYYY') YEAR,TO_CHAR(T.MODIFIEDTIME,'Q') TIME,COUNT(*) COUNT
FROM TEST T
--這裏可加查詢條件 WHERE TO_CHAR(T.MODIFIEDTIME,'YYYY') = TO_CHAR(SYSDATE,'YYYY')
GROUP BY TO_CHAR(T.MODIFIEDTIME,'Q'),TO_CHAR(T.MODIFIEDTIME,'YYYY') --根據每年季度來分組
ORDER BY TO_CHAR(T.MODIFIEDTIME,'YYYY'),TO_CHAR(T.MODIFIEDTIME,'Q') ASC NULLS LAST --根據每年季度來排序
--按年統計
SELECT TO_CHAR(T.MODIFIEDTIME,'YYYY') YEAR,COUNT(*) COUNT
FROM TEST T
--這裏可加查詢條件
GROUP BY TO_CHAR(T.MODIFIEDTIME,'YYYY') --根據年份來分組
ORDER BY TO_CHAR(T.MODIFIEDTIME,'YYYY') ASC NULLS LAST --根據年份來排序
看到這,相信你已經知道規律了吧。
日期和字符轉換函數用法(to_date,to_char)
selectto_char(sysdate,'yyyy-mm-dd hh24:mi:ss')asnowTimefromdual; //日期轉化為字符串
selectto_char(sysdate,'yyyy')asnowYear fromdual; //獲取時間的年
selectto_char(sysdate,'mm') asnowMonthfromdual; //獲取時間的月
selectto_char(sysdate,'dd') asnowDay fromdual; //獲取時間的日
selectto_char(sysdate,'hh24')asnowHour fromdual; //獲取時間的時
selectto_char(sysdate,'mi') asnowMinutefromdual; //獲取時間的分
selectto_char(sysdate,'ss') asnowSecondfromdual; //獲取時間的秒
--按月查詢(ny not like '%00'/ubstr(ny,5,2)!='00'最後兩位為00不顯示)
select ny,ycyl from(
select to_char(add_months(sysdate,-13), 'yyyymm')qf,ny,ycyl from (
select ny,sum(ycyl)ycyl from ktkfsjzx.YA1_YA01066
group by ny order by ny desc))where ny>qf and substr(ny,5,2)!='00'
--ny not like '%00'
--按月查詢
SELECT TO_CHAR(T.rq,'YYYY-MM')
rq,sum(rcyl)rcyl,sum(rcyl1)rcyl1,sum(yjs)yjs,sum(ykjs)ykjs,sum(sjs)sjs,sum(skjs)skjs
FROM scsjb t GROUP BY TO_CHAR(T.rq,'YYYY-MM')
ORDER BY TO_CHAR(T.rq,'YYYY-MM') DESC NULLS LAST
--當月往前推9個月總數據查詢
select rq,rcyl,rcyl1,yjs,ykjs,sjs,skjs from(
select to_char(add_months(sysdate,-9), 'yyyy-mm')qf,rq,rcyl,rcyl1,yjs,ykjs,sjs,skjs from (
SELECT TO_CHAR(T.rq,'YYYY-MM') rq,sum(rcyl)rcyl,sum(rcyl1)rcyl1,sum(yjs)yjs,sum(ykjs)ykjs,sum(sjs)sjs,sum(skjs)skjs
FROM scsjb t GROUP BY TO_CHAR(T.rq,'YYYY-MM')
ORDER BY TO_CHAR(T.rq,'YYYY-MM') DESC NULLS LAST)) where rq>qf
--按季度查詢
SELECT TO_CHAR(T.rq,'YYYY')
rq,sum(rcyl)rcyl,sum(rcyl1)rcyl1,sum(yjs)yjs,sum(ykjs)ykjs,sum(sjs)sjs,sum(skjs)skls
FROM scsjb t GROUP BY TO_CHAR(T.rq,'YYYY'),TO_CHAR(T.rq,'Q')
ORDER BY TO_CHAR(T.rq,'YYYY')DESC NULLS LAST,TO_CHAR(T.rq,'Q') ASC NULLS LAST
--按年查詢
SELECT TO_CHAR(T.rq,'YYYY')
rq,sum(rcyl)rcyl,sum(rcyl1)rcyl1,sum(yjs)yjs,sum(ykjs)ykjs,sum(sjs)sjs,sum(skjs)skls
FROM scsjb t GROUP BY TO_CHAR(T.rq,'YYYY')
ORDER BY TO_CHAR(T.rq,'YYYY')DESC NULLS LAST
Oracle按日期統計