1.我用的是PLSQL Developer连接的Oracle数据库
2.我想从表T2011中按照某条件选出某几行进行操作
3.将选出的这几行进行排序
4.用上一行某列的值减去下一行某列的值关键的第4步,应该怎么写?表T2011
列A  列B 列C
2001  1   2
2002  8   4
2003  6   6
2004  9   8
……
……
……
我想用每行的B列的值减去下一行C列的值并且输出输出为
-3
2
-2
……
……
……

解决方案 »

  1.   


    select b-lead(c) over(order by a) from temp
      

  2.   


    我是新手 请问Temp 是什么?
      

  3.   


    with tb as(
    select 2001 a,1 b,2 c from dual union all
    select 2002, 8, 4 from dual union all
    select 2003, 6, 6 from dual union all
    select 2004, 9, 8 from dual)
    select a,b,c,lead(c) over(order by a),b-lead(c) over(order by a)
    from tb       A          B          C LEAD(C)OVER(ORDERBYA) B-LEAD(C)OVER(ORDERBYA)
    -------- ---------- ---------- --------------------- -----------------------
        2001          1          2                     4                      -3
        2002          8          4                     6                       2
        2003          6          6                     8                      -2
        2004          9          8
      

  4.   


    with tb as(
    select 2001 a,1 b,2 c from dual union all
    select 2002, 8, 4 from dual union all
    select 2003, 6, 6 from dual union all
    select 2004, 9, 8 from dual)
    --这些是提供数据的语句 相当于临时表
      

  5.   


    cool!~~  此外发现此人很喜欢用with as的写法哈~
      

  6.   

    tb 为你前面三步的结果集select cola,colb-lead(colc,1,0) over(order by cola),colc
    from tb
      

  7.   

    上面的代码非常精炼!!很好
        select b-lead(c,1) over(order by a) from table