Oracle按照年月日分组

  • 按年统计
SELECT TO_CHAR(A.SETTLE_TIME,'YYYY') TIME,COUNT(*) COUNT,SUM(A.AL_NUM)
FROM R_MEAS_BASIC_INFO A,D_STAKE_ARCH B   
WHERE A.STAKE_NO=B.EQUIP_NO AND B.ORG_NO='100000302'
AND TO_CHAR(A.SETTLE_TIME,'YYYY') = TO_CHAR(SYSDATE,'YYYY')--查询本年度的
GROUP BY TO_CHAR(A.SETTLE_TIME,'YYYY')   --根据年份来分组
ORDER BY TO_CHAR(A.SETTLE_TIME,'YYYY') ASC NULLS  LAST;  --根据年份来排序
  • 按月统计
SELECT TO_CHAR(A.SETTLE_TIME,'YYYY-MM') TIME,COUNT(*) COUNT,SUM(A.AL_NUM)
FROM R_MEAS_BASIC_INFO A,D_STAKE_ARCH B   
WHERE A.STAKE_NO=B.EQUIP_NO AND B.ORG_NO='100000302'
AND TO_CHAR(A.SETTLE_TIME,'YYYY') = TO_CHAR(SYSDATE,'YYYY')--查询本年度的
GROUP BY TO_CHAR(A.SETTLE_TIME,'YYYY-MM')   --根据月份来分组
ORDER BY TO_CHAR(A.SETTLE_TIME,'YYYY-MM') ASC NULLS  LAST;  --根据月份来排序
  • 按天统计
SELECT TO_CHAR(A.SETTLE_TIME,'YYYY-MM-DD') TIME,COUNT(*) COUNT,SUM(A.AL_NUM)
FROM R_MEAS_BASIC_INFO A,D_STAKE_ARCH B   
WHERE A.STAKE_NO=B.EQUIP_NO AND B.ORG_NO='100000302'
AND TO_CHAR(A.SETTLE_TIME,'YYYY') = TO_CHAR(SYSDATE,'YYYY')--查询本年度的
GROUP BY TO_CHAR(A.SETTLE_TIME,'YYYY-MM-DD')   --根据天来分组
ORDER BY TO_CHAR(A.SETTLE_TIME,'YYYY-MM-DD') ASC NULLS  LAST;

你可能感兴趣的:(Oracle)