2016-02-04 39 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' 
相關問題