2016-02-04 37 views
0

这里是我的表 enter image description here更新两列由同一表下一行的另一列在SQL

而且我的输出应该是 enter image description here

我想未来Update_date,Update_time更新closed_date,timenew_class_desc ='FLM'但如果new_class_desc'FollowupComments'则忽略它并更新下一个日期为Closed_date

我试图查询有点像这样..

;WITH cte as(
    SELECT * 
    ,row_number() OVER(ORDER BY Update_date,Update_time) rn 
    FROM Table 
    WHERE Problem_sid = 1435819 
) 
UPDATE c1 SET Closed_date = c2.Update_date, Closed_time = c2.Update_time 
FROM cte c1 
JOIN cte c2 ON c1.rn = c2.rn - 1 
    AND c1.New_class_desc = 'FLM' 
    AND c2.New_class_desc <> 'FLM' 
    AND c2.New_class_desc not in ('FollowUpComments') 

但是在这个我没有得到new_class_desc =Bank更新日期为Closed_date Flm。

请在这里指导。

回答

0
WITH cte 
AS 
(
    SELECT *, ROW_NUMBER() OVER(ORDER BY t.Update_date, t.Update_time) AS rno 
    FROM my_Table t 
    WHERE t.New_class_desc <> 'FollowUpComments' 
) 
UPDATE t 
SET t.Closed_Date = t1.Closed_Date, 
t.Closed_Time = t1.Closed_Time 
FROM cte t 
JOIN cte t1 ON t.rno = t1.rno + 1 
WHERE t.New_class_desc = 'FLM' 
相关问题