1. 程式人生 > >sqlserver資料庫中sql的使用

sqlserver資料庫中sql的使用

1. 分組排序更新

將分組記錄中的第一條資料的某個欄位更新到第二條資料上的欄位;

--把序號為2的結束時間 更新為 序號為1的開始時間
WITH t2 as (
    SELECT * FROM(
        SELECT 
            ROW_NUMBER() OVER ( partition by C_BH_ZFXX ORDER BY DT_KSSJ DESC) as num ,
            C_BH_ZFXX,
            DT_KSSJ,
            DT_JSSJ,
            C_BH
            
FROM DB_JY..T_FJFY_FJJL WHERE --c_bh_zfxx in( '046B7C4311C0413FBD8A5B5EDC5C9460','087CE841726B43209439B09E87AE8C48') AND DT_JSSJ is NULL )b WHERE b.num=2 ) UPDATE t2 SET t2.dt_jssj = t1.dt_kssj FROM( SELECT * FROM( SELECT ROW_NUMBER() OVER ( partition by
C_BH_ZFXX ORDER BY DT_KSSJ DESC) as num , C_BH_ZFXX, DT_KSSJ, DT_JSSJ, C_BH FROM DB_JY..T_FJFY_FJJL WHERE -- c_bh_zfxx in( '046B7C4311C0413FBD8A5B5EDC5C9460','087CE841726B43209439B09E87AE8C48') AND DT_JSSJ is NULL
) a WHERE a.num = 1 ) t1 WHERE t1.C_BH_ZFXX = t2.C_BH_ZFXX