我现在做一个统计查询 做一个jfreechart图表表字段如下
code --编号
slslrq --日期
我现在主要就是根据这个日期进行统计
查询指定日期范围内的 每个月的数量。
我是这样写的。
select to_char(slslrq,'yyyy-mm') as rq,count(rowid) as num from Jjsldj where
substr(ajcode, 2, 2) like '33'
and slslrq between add_months(to_date('2009-10-20 11:59:59','YYYY-MM-DD HH:MI:SS'),-12)
and to_date('2009-10-20 11:59:59','YYYY-MM-DD HH:MI:SS') and 1=1 group by to_char(slslrq,'yyyy-mm')
输出
rq num
2008-11 3
2009-1 2
2009-3 5
因为有些月份可能没有数据,所有按月分组 没有那个月作为条件
我想得到的是
rq num
2008-11 3
2008-12 0
2009-1 2
2009-2 0
2009-3 5之前弄了几种解决办法都不是很好。希望各位能指导下小弟 非常感谢。
code --编号
slslrq --日期
我现在主要就是根据这个日期进行统计
查询指定日期范围内的 每个月的数量。
我是这样写的。
select to_char(slslrq,'yyyy-mm') as rq,count(rowid) as num from Jjsldj where
substr(ajcode, 2, 2) like '33'
and slslrq between add_months(to_date('2009-10-20 11:59:59','YYYY-MM-DD HH:MI:SS'),-12)
and to_date('2009-10-20 11:59:59','YYYY-MM-DD HH:MI:SS') and 1=1 group by to_char(slslrq,'yyyy-mm')
输出
rq num
2008-11 3
2009-1 2
2009-3 5
因为有些月份可能没有数据,所有按月分组 没有那个月作为条件
我想得到的是
rq num
2008-11 3
2008-12 0
2009-1 2
2009-2 0
2009-3 5之前弄了几种解决办法都不是很好。希望各位能指导下小弟 非常感谢。
from dual connect by rownum <= 12把这个查询当成一个表,右连你的表
from Jjsldj a,
(select add_months(to_date('2009-10','yyyy-mm-dd'),rownum-13)rq from dual
connect by rownum<=13)b
where b.rq=trunc(a.slslrq(+),'mm')
and substr(a.ajcode(+), 2, 2) = '33'
and a.slslrq(+) between add_months(to_date('2009-10-20 11:59:59','YYYY-MM-DD HH:MI:SS'),-12)
and to_date('2009-10-20 11:59:59','YYYY-MM-DD HH:MI:SS') and 1=1 group by b.rq
from Jjsldj a,
(select add_months(to_date('2009-10','yyyy-mm-dd'),rownum-13)rq from dual
connect by rownum<=13)b
where b.rq=trunc(a.slslrq(+),'mm')
and substr(a.ajcode(+), 2, 2) = '33'
and a.slslrq(+) between add_months(to_date('2009-10-20 11:59:59','YYYY-MM-DD HH:MI:SS'),-12)
and to_date('2009-10-20 11:59:59','YYYY-MM-DD HH:MI:SS') and 1=1 group by b.rq
因为在count(1)的情况下,右连,因为左边那个月份表的存在,至少都会有一条记录的,换成count(jjsldj)的字段后,右表不存在的将会被记null,不会纳入计算所以还需要用nvl(count(b.rowid),0)处理以下。