How to update record based on value of next record in SQL Server 2008

后端 未结 2 466
礼貌的吻别
礼貌的吻别 2021-01-26 17:21

I like to update a column based on a value of the next record:

resid   startdate   enddate     weekday hours
-----------------------------------------------
2980         


        
2条回答
  •  一生所求
    2021-01-26 17:44

    One way to do it is with a subquery:

    UPDATE t1
    SET enddate = (SELECT TOP 1 DATEADD(DAY, -1, startdate) 
                   FROM YourTable t2 
                   WHERE t1.startdate < t2.startdate 
                   ORDER BY t2.startdate)
    FROM YourTable t1
    WHERE t1.enddate is null
    

提交回复
热议问题