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

后端 未结 2 467
礼貌的吻别
礼貌的吻别 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
    
    0 讨论(0)
  • 2021-01-26 17:46
    update TableName set enddate = dateadd(day,1,startdate)
    
    0 讨论(0)
提交回复
热议问题