提货序号    提货数  库存数   剩余数
  1            3      15      12
  2            5      15      7
  3            1      15      6
  4            4      15      2
  5            6      15      -4
  6            2      15      -6
  7            2      15      -8
  8            1      15      -9
原始数据为 提货序号 提货数 库存数 需要得到上表剩余数 
Oracle 9i 

解决方案 »

  1.   

    select 提货序号,提货数,库存数,库存数-提货数 as 剩余数 from tablename;
      

  2.   

    另 补充下,因为此为部分数据还需后续处理,因此不想用cursor处理此部分数据得到结果
      

  3.   

    select 提货序号,提货数,库存数,库存数-提货数 as 剩余数 from tablename;
      

  4.   

    看下面代码,已经测试过了select 提货序号,
           提货数,
           库存数,
           库存数-sum(提货数) over (order by 提货序号)
    from   tab;
      

  5.   

    with temp as(
    select 1 a,3 b,15 c from dual
    union all
    select 2 a,5 b,15 c from dual
    union all
    select 3 a,1 b,15 c from dual
    union all
    select 4 a,4 b,15 c from dual
    union all
    select 5 a,6 b,15 c from dual
    union all
    select 6 a,2 b,15 c from dual
    union all
    select 7 a,2 b,15 c from dual
    union all
    select 8 a,1 b,15 c from dual
    )
    select a,b,c,c - sum(b) over(order by a) d from temp
      

  6.   

    感谢楼上这位,一直想着lead lag开窗 ,没想到sum开窗 ,分给你...
      

  7.   

    Phoenix_99 不好意思了,我结贴跟你差不多同时...没看到你回帖了...