其实非常简单,却让人头疼!
资料:
table:
     month   total
      3       50
      5       60
      6       80
用sql实现
      month   total
       1        0
       2        0
       3        50
       4        0
      ..............
      ..............
我的开发环境受限制,不能新建表和用存储过程!
sql好难写啊!有没有好办法!有兴趣话请看下面的问题!
以上我把问题简单化其实:
  table:
         date1       number 
         2001-6-12      36
         2001-12-23     23
         2002-2-3       60
         2002-2-25      20
         2002-4-6       45
         2002-4-27      30查询  2002.1 到2002.5
实现: 
        month        totol   increase/decrease
     (去年12)12        23         0 
           1           0         -23
           2            80        +57
           3            0         -80
           4           75         +75
           5            0         -75高手们留下您的墨宝吧! 
                

解决方案 »

  1.   

    select rownum,tb.total from tb,ALL_SOURCE
    where all_source.rownum=tb.rownum(+) and rownum<(select max(month) from tb);
      

  2.   

    select a.rn,nvl(b.total,0) from 
    (select rownum rn from dba_objects where rownum<=12)a,table b,
    where a.rn=b.month(+)
      

  3.   

    select rownum month,totol,totol-lag(totol,1,0) over(order by date1) increase/decrease from 
    (select to_char(date1,'yyyymm') date1,sum(number) totol from table where date1 between to_date('2002-01','yyyy-mm') and to_Date('2002-05','yyyy-mm') group by to_char(date1,'yyyymm'))
      

  4.   

    大虾们:
           查询  2002.1 到2002.5 这个只是我举个例子,其实是差数,从网页上传来的!
           而且2001.12 月也是我大个比喻,在实际情况,数据库中可能没有2001.12记录!
           但是查询2002.1 到2002.5 时 要把去年的2001.12 输出! 
           而且当中月没有记录也要显示。       
            month        totol   increase/decrease
         (去年12)12         0           0 
               1            0          0
               2            80        +80
              3             0         -80
               4            75         +75
               5             0         -75