Hi All, Could you let me know how to achieve for below logic:- I need to flag the below records from table 1 in such a way that, I need to get the second table Flag column populated. Data is sorted by column 1,2. Scenario : should be flagged as 1 for first visit and for remaining visits we need to --> compare the current visit with the previous visit based on the Reason column. If records have same reason, then compare to prior visit Reason (Until 3 times, only if Reason value is same). Please see the logic column in the Desired outcome table. Patient_ID Visit Reason Flag 500 1 Heart 500 2 Kidney 500 3 Lungs 500 4 Lungs 500 5 Lungs Desired Outcome: Patient_ID Visit Reason Flag Logic 500 1 Heart 1 since first visit we will flag as 1 500 2 Kidney 1 comparing current visit (Kidney) with previous (Heart), flagging as 1 when different value 500 3 Lungs 1 comparing current visit (Lungs) with previous (Kidney), flagging as 1 when different value 500 4 Lungs 0 comparing current visit (Lungs) with previous (Lungs), when same value, compare with prior value which is (Kidney)(2nd visit), since it is different we will assign 0 500 5 Lungs 1 comparing current visit (Lungs) with previous (Lungs), when same value, compare with prior value which is (Lungs)(3rd visit), since it is same we will assign 1 Note:- we need to compare only until last 3 visits based on Reason column. Thanks Lavanya
... View more