1个回答
展开全部
--1、考虑到没有数据的也要总结出一条0的记录,那我们需要先生成一张当月的日期中间表
SELECT FDATE
FROM (SELECT TRUNC(SYSDATE, 'MONTH') + LEVEL - 1 AS FDATE
FROM DUAL
CONNECT BY LEVEL <= 31) T
WHERE TO_CHAR(FDATE, 'MM') = TO_CHAR(SYSDATE, 'MM')
--2、以上边的表作为左链接的表,与你的数据表的日期做关联查询就可以得出你要的结果
SELECT A.FDATE, NVL(B.FNUM, 0)
FROM (SELECT FDATE
FROM (SELECT TRUNC(SYSDATE, 'MONTH') + LEVEL - 1 AS FDATE
FROM DUAL
CONNECT BY LEVEL <= 31) T
WHERE TO_CHAR(FDATE, 'MM') = TO_CHAR(SYSDATE, 'MM')) A
LEFT JOIN (SELECT 日期字段名 AS FDATE, COUNT(1) AS FNUM
FROM 表名 B
WHERE TO_CHAR(日期字段名, 'YYYYMM') = TO_CHAR(SYSDATE, 'YYYYMM')
GROUP BY 日期字段名) B
ON A.FDATE = B.FDATE
ORDER BY A.FDATE
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询