年         月        公司         本年累计
        2010        1        a有限公司        473.9170
        2010        2        a有限公司        801.2761
        2010        3        a有限公司        1205.8871
        2010        4        a有限公司        1600.5985
        2010        5        a有限公司        1963.9508
        2010        6        a有限公司        2336.8037
        2010        7        a有限公司        2591.0544
        2010        8        a有限公司        2987.3007
        2010        9        a有限公司        3349.8111
        2010        10        a有限公司        3693.0838
        2010        11        a有限公司        4051.8527
        2010        12        a有限公司        4448.7825通过本年累计,求本月,每一列都是上月的累加
脑子绕进取了,求高手帮忙.
在线等

解决方案 »

  1.   

    select m.* , m.本年累计 - nvl(n.本年累计 , 0) 本月
    from tb m left join tb n
    on m.年 = n.年 and m.公司 = n.公司 and m.月 = n.月 + 1
      

  2.   

    select h.月,
           h.本年累计 as formerval,
           lag(h.本年累计) over(order by h.月) as lastval
      from tableName h;
      

  3.   

    tb 你的表……
    select tb.*,nvl(本年累计-prior 本年累计,0) from tb start with 月=1 connect by 月 = prior 月+1
      

  4.   


     oracle自带的函数 使用方便一点
      

  5.   

    每一列都是上月的累加,
    为什么不是sum ?select h.月,
           h.本年累计 as formerval,
           sum(h.本年累计) over(order by h.月) as lastval
      from tableName h;
      

  6.   

    h.月,h.formerval,
    sum(h.lastval) over(oder by h.月)
    (select h.月,
      h.本年累计 as formerval,
      lag(h.本年累计,1,0) over(order by h.月) as lastval
      from tableName h
    ) h
    是这个意思吧?
      

  7.   

       with t as
    (
    select   2010 year, 1 month ,'a有限公司' com, 473.9170 je     from dual union all
    select   2010 ,2 ,'a有限公司', 801.2761     from dual union all
    select   2010 ,3 ,'a有限公司', 1205.8871    from dual union all
    select   2010 ,4 ,'a有限公司', 1600.5985    from dual union all
    select   2010 ,5 ,'a有限公司', 1963.9508    from dual union all
    select   2010 ,6 ,'a有限公司', 2336.8037    from dual union all
    select   2010 ,7 ,'a有限公司', 2591.0544    from dual union all
    select   2010 ,8 ,'a有限公司', 2987.3007    from dual union all
    select   2010 ,9 ,'a有限公司', 3349.8111    from dual union all
    select   2010 ,10, 'a有限公司', 3693.0838   from dual union all
    select   2010 ,11, 'a有限公司', 4051.8527   from dual union all
    select   2010 ,12, 'a有限公司', 4448.7825   from dual union all
    select   2011 ,01, 'a有限公司', 5555.55   from dual  union all 
    select   2011 ,01, 'a有限公司', 6666.55   from dual   union all 
    select   2009 ,01, 'a有限公司', 10000   from dual  
    )
    select year,month,com,je,je-nvl(lag(je) over (partition by year order by year,month),0) from t