日期:2014-05-17 浏览次数:20850 次
with t1 as ( select 'A110' c1,date'2012-09-28' c2 from dual union all select 'A100' c1,date'2012-07-09' c2 from dual union all select 'A110' c1,date'2012-10-01' c2 from dual union all select 'A110' c1,date'2012-10-01' c2 from dual union all select 'A110' c1,date'2012-10-03' c2 from dual union all select 'A110' c1,date'2012-10-05' c2 from dual union all select 'A120' c1,date'2012-10-01' c2 from dual union all select 'A000' c1,date'2012-10-03' c2 from dual ) select c.c1,c.t_date,count(d.c2) c_num from ( select a.c1,b.t_date from ( select distinct c1 from t1 where c2 between trunc(date'2012-10-03','month') and date'2012-10-03' ) a, ( select trunc(date'2012-10-03','month')+level-1 t_date from dual connect by level <= date'2012-10-03'-trunc(date'2012-10-03','month')+1 ) b ) c left join t1 d on c.c1 = d.c1 and c.t_date = d.c2 group by c.c1,c.t_date order by c.c1,c.t_date c1 t_date c_num -------------------------------------- 1 A000 2012/10/1 0 2 A000 2012/10/2 0 3 A000 2012/10/3 1 4 A110 2012/10/1 2 5 A110 2012/10/2 0 6 A110 2012/10/3 1 7 A120 2012/10/1 1 8 A120 2012/10/2 0 9 A120 2012/10/3 0
------解决方案--------------------
[code=SQL][/code]
with t1 as
(
select 'A000' c1,date'2012-10-03' c2,1 c3 from dual
union all
select 'A100' c1,date'2012-07-09' c2,2 c3 from dual
union all
select 'A100' c1,date'2012-10-01' c2,3 c3 from dual
union all
select 'A100' c1,date'2012-10-03' c2,4 c3 from dual
union all
select 'A100' c1,date'2012-10-05' c2,5 c3 from dual
union all
select 'A110' c1,date'2012-08-09' c2,6 c3 from dual
union all
select 'A110' c1,date'2012-10-01' c2,7 c3 from dual
union all
select 'A110' c1,date'2012-10-02' c2,8 c3 from dual
union all
select 'A110' c1,date'2012-10-04' c2,9 c3 from dual
union all
select 'A120' c1,date'2012-10-01' c2,5 c3 from dual
union all
select 'A120' c1,date'2012-10-02' c2,7 c3 from dual
union all
select 'A120' c1,date'2012-10-03' c2,3 c3 from dual
)
select t4.c1,t4.c2,nvl(t1.c3,0) from t1,
(select * from
(select distinct c1 from t1)t2,
(select date'2012-10-06' + 1 - level c2 from dual connect by level <= to_number(to_char(date'2012-10-06','dd')))t3
)t4
where t1.c1(+) = t4.c1 and t1.c2(+) = t4.c2
1 A000 2012/10/1 0
2 A000 2012/10/2 0
3 A000 2012/10/3 1
4 A000 2012/10/4 0
5 A000 2012/10/5 0
6 A000 2012/10/6 0
7 A100 2012/10/1 3
8 A100 2012/10/2 0
9 A100 2012/10/3 4
10 A100 2012/10/4 0
11 A100 20