日期:2014-05-17  浏览次数:20884 次

求大神赐于一条SQL,感激不尽
oracle有数据表是T
DYEARMONTH PK_CORP CWAREHOUSEID CINVENTORYID            CINVBASID VAR
2012-04 1002 1002F8100000000003ZA 0001A210000000008K16 0001F8100000000009ZZ 70
2012-05 1002 1002F8100000000003ZA 0001A210000000008K16 0001F8100000000009ZZ -25
2012-06 1002 1002F8100000000003ZA 0001A210000000008K16 0001F8100000000009ZZ -10
2012-07 1002 1002F8100000000003ZA 0001A210000000008K16 0001F8100000000009ZZ 72
2012-08 1002 1002F8100000000003ZA 0001A210000000008K16 0001F8100000000009ZZ -3
2012-09 1002 1002F8100000000003ZA 0001A210000000008K16 0001F8100000000009ZZ -37
2012-10 1002 1002F8100000000003ZA 0001A210000000008K16 0001F8100000000009ZZ -27
2012-11 1002 1002F8100000000003ZA 0001A210000000008K16 0001F8100000000009ZZ -17
2012-12 1002 1002F8100000000003ZA 0001A210000000008K16 0001F8100000000009ZZ 91
2013-01 1002 1002F8100000000003ZA 0001A210000000008K16 0001F8100000000009ZZ -47

现在想有一个类似递归的形式,以DYEARMONTH为条件,如2012-04这条数据VAR的值是本身(70),2012-05的数据为
2012-04的var+上2012-05的var,70+(-25),一下类似,重新整理输出数据


结果如:
2012-04 1002 1002F8100000000003ZA 0001A210000000008K16 0001F8100000000009ZZ 70
2012-05 1002 1002F8100000000003ZA 0001A210000000008K16 0001F8100000000009ZZ 45
2012-06 1002 1002F8100000000003ZA 0001A210000000008K16 0001F8100000000009ZZ 35
2012-07 1002 1002F8100000000003ZA 0001A210000000008K16 0001F8100000000009ZZ 107
。。。。。。。。

求高手解决,在线等,谢谢!!!







------解决方案--------------------
try this,

select a.DYEARMONTH,
       a.PK_CORP,
       a.CWAREHOUSEID,
       a.CINVENTORYID,
       a.CINVBASID,
       nvl((select sum(b.VAR) from tabT b
            where TO_DATE(b.DYEARMONTH+'-01','YYYY-MM-DD')-TO_DATE(a.DYEARMONTH+'-01','YYYY-MM-DD')<0),0)+a.VAR 'VAR'
 from tabT a