Example:
update a end_dt column which is null based on next created record created date
CURRENT SITUTAION:
CUST_ID CREATED_DT END_DT
1 3/25/2013 6:51:33 PM
1 3/25/2013 6:23:58 PM
1 3/25/2013 5:02:07 PM
1 3/12/2013 9:18:21 AM
1 3/8/2013 12:47:13 PM
1 1/25/2013 7:24:02 AM
1 1/23/2013 11:26:31 AM
1 11/13/2012 8:55:31 AM
NEEDED:
CUST_ID CREATED_DT END_DT
1 3/25/2013 6:51:33 PM
1 3/25/2013 6:23:58 PM 3/25/2013 6:51:33 PM
1 3/25/2013 5:02:07 PM 3/25/2013 6:23:58 PM
1 3/12/2013 9:18:21 AM 3/25/2013 5:02:07 PM
1 3/8/2013 12:47:13 PM 3/12/2013 9:18:21 AM
1 1/25/2013 7:24:02 AM 3/8/2013 12:47:13 PM
1 1/23/2013 11:26:31 AM 1/25/2013 7:24:02 AM
1 11/13/2012 8:55:31 AM 1/23/2013 11:26:31 AM
Assuming SQL Server:
UPDATE c
SET END_DT=(SELECT TOP 1 CREATED_DT FROM CustDates WHERE CUST_ID=c.CUST_ID AND
CREATED_DT>c.CREATED_DT ORDER BY CREATED_DT)
FROM CustDates c
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.