1

例子:

根据下一个创建记录创建日期更新一个为空的 end_dt 列

当前情况:

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   

需要:

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
4

1 回答 1

1

假设 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
于 2013-04-03T11:53:14.360 回答