样例数据:
XLBH   SZGJFX   LDSXH   SIGN
101      0        10     L0
101      0        20     L
101      0        30     L
101      0        40     L1
101      1        10     L0
101      1        20     L
101      1        30     L
101      1        40     L1102      0        10     L0
102      0        20     L
.........
102      0        200    L1
102      1        10     L0
102      1        20     L
102      1        30     L
........
102      1        260    L1L0代表该线路的起始路段标识,L1代表该线路的终止路段标识,L:代表该线路的途径路段标识
可以使用update a_ldly set sign ='L0' WHERE LDSXH=10;
请问如可写SQL语句,才能使得某条线路在某个方向(0:上行/1:下行)时候的MAX(LDSXH)=L1???

解决方案 »

  1.   

    update a_ldly a set sign ='L1' where exists (select 1 from (select * from (select XLBH, SZGJFX, max(LDSXH) end_tag from a_ldly group by XLBH, SZGJFX)) b where a.XLBH=b.XLBH and a.SZGJFX=b.SZGJFX and a.LDSXH=b.end_tag);
      

  2.   

    update a_ldly a set sign ='L1' where rowid in (select rowid from (select rowid, XLBH, SZGJFX, LDSXH, row_number() over(partition by XLBH, SZGJFX order by SZGJFX desc) r_no from a_ldly) where r_no =1);
      

  3.   


    select *
      from a_ldly
     where rowid in (select rowid
                       from (select rowid,
                                    row_number() over(partition by szgjfx order by ldsxh desc) rn
                               from a_ldly) t
                      where rn = 1)