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

时间:2013-04-03 11:30:09

标签: sql

示例:

根据下一个创建的记录创建日期

更新end_dt列,该列为空

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   

需要:

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

1 个答案:

答案 0 :(得分: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