ZDSXH  LDSXH   ZDSXH2
10      10       
20      30       
30      50       
40      70       
50      80       
51      80        
60      100      
70      140      
80      140 
90      140求SQL语句,使得ZDSXH2为以下结果:
9
29
49
69
78
79
99
137
138
139即若LDSXH唯一的话,ZDSXH2就=LDSXH-1
如果不唯一,就代表这个路段上有2个以上站点(N个站点),我要让他按照ZDSXH的值大小分别=LDSXH-N,LDSXH-(N-1),LDSXH-(N-2)……

解决方案 »

  1.   

    select ZDSXH, LDSXH,LDSXH-val ZDSXH2 from
    (select t.ZDSXH,t.LDSXH,row_number() over(partition by t.LDSXH order by t.ZDSXH desc) val from test_table t)
    order by ZDSXH;
      

  2.   

    select zdsxh,ldsxh,ldsxh-v
    from(
    select zdsxh,ldsxh,count(*) over(partition by ldsxh order by zdsxh desc) v from tt_03
    )
    order by zdsxh
      

  3.   


      1  select ldsxh-rn zdsxh2
      2  from
      3  (
      4  select row_number() over(partition by ldsxh order by zdsxh2) rn,ldsxh
      5  from t1
      6  )
      7* order by zdsxh2
    SQL> /    ZDSXH2
    ----------
             9
            29
            49
            69
            78
            79
            99
           137
           138
           139已选择10行。