Hi @Suman Gupta
You can try this query.
;with T1 as(
select *,row_number()over(order by ID2,UNQ_ID desc) as num from Source
),T2 as(
select A.UNQ_ID,A.ID2,A.ID3,
case when B.Date2 = '12/31/99' or B.Date2 is null then A.Date1 else B.Date2 end as Date1,
A.Date2,A.Active_Flg
from T1 as A left outer join T1 as B on A.num = B.num - 1)
select * into Final_result from T2;
select * from Final_result;
Best regards,
Percy Tang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.