我有一张桌子:
locid timestart timeend
1 Jan 23 2015 Dec-31-9999
1 Feb 15 2015 Dec-31-9999
2 Mar 18 2015 Apr-28-2015
2 Nov 23 2015 Dec-31-9999
2 Jul 23 2015 Nov-23-2015
2 Apr 28 2015 Dec-31-9999
上表中的时间结束不正确。它应该像下面的
locid timestart timeend
1 Jan 23 2015 feb-15-2015
1 Feb 15 2015 Dec-31-9999
2 Mar 18 2015 Apr-28-2015
2 Nov 23 2015 Jul-23-2015
2 Jul 23 2015 Nov-23-2015
2 Apr 28 2015 Dec-31-2015
如何编写SQL来纠正表1中的时间结束,如表2所示?
答案 0 :(得分:1)
您想要下一行的timestart
:
WITH newTable AS
(SELECT 8, LEAD(timestart,1,'99991231') OVER (PARTITON BY locid ORDER BY timestart) AS newTimeEnd
FROM theTable
)
UPDATE newTable SET timeend = newTimeEnd;