不管接下来发生什么事,你都不能停止前进

Oracle按日期统计

--创建表 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
posted @ 2019-05-10 22:18  普罗旺斯_S  阅读(4349)  评论(0编辑  收藏  举报