table:
例:
year   month    errors2009     2         32009     5         2
想要的结果:
1.2009 1 0
2.2009 2 3
3.2009 3 0
4.2009 4 0
5.2009 5 2....
就是想没有的月份填充0
谢谢 

解决方案 »

  1.   

    with tt as(select 2009 year,2 month,3 errors from dual
      union all select 2009,5,2 from dual)
      
    select a.year,a.month,nvl(b.errors,0) from
    (select a1.year,a2.month from
        (select distinct year from tt)a1,
         (select rownum month from dual connect by rownum<=12)a2)a,
     tt b
    where a.year=b.year(+) and a.month=b.month(+)
    order by a.year,a.month
      

  2.   

    with temp as
    (
    select 2009 year,    2 month,        3 errors from dual
    union all 
    select 2009,    5,        2 from dual
    )
    select nvl(year,b.a),nvl(month,b.b),nvl(errors,0) from temp a,
    (select to_char(sysdate,'yyyy') a,to_char(rownum,'00') b from dual connect by rownum<13)b
    where b.a=a.year(+) and b.b=a.month(+)--result:2009 1 0
    2009 2 3
    2009 3 0
    2009 4 0
    2009 5 2
    2009 6 0
    2009 7 0
    2009 8 0
    2009 9 0
    2009 10 0
    2009 11 0
    2009 12 0