ID Name Request Date
296459 Waiting 2016-06-04 22:53:46.000
296459 ED-22 2016-06-04 23:01:30.650
296459 CCS-CC28-A 2016-06-05 01:16:00.000
296459 4N-N450-A 2016-06-14 06:33:41.530
我想创建一个名为End Date的新行,它将包含下一行的Request Date。
ID Name Request Date End Date
296459 Waiting 2016-06-04 22:53:46.000 2016-06-04 23:01:30.650
296459 ED-22 2016-06-04 23:01:30.650 2016-06-05 01:16:00.000
296459 CCS-CC28-A 2016-06-05 01:16:00.000 2016-06-14 06:33:41.530
296459 4N-N450-A 2016-06-14 06:33:41.530 GETDATE()
答案 0 :(得分:3)
如果您使用的是SQL Server 2012+,请使用lead
获取下一行的值(基于指定的顺序)
select t.*,
coalesce(lead(request_date) over(partition by id order by request_date), getdate()) as end_date
from tablename t