I'm sure this is answered somewhere, but I'm not quite sure how to search for it.
I have a table, [tbl_Data], that looks like this:
ID| From_Dt | To_Dt |
1 |01/01/2016 | 01/03/2016|
1 |01/04/2016 | 01/09/2016|
1 |01/09/2016 | 01/20/2016|
Want the output to look like:
ID|Prev_From_Dt|Prev_To_Dt |Recent_From_Dt|Recent_To_Dt|
1 |01/01/2016 |01/03/2016 |01/04/2016 |01/09/2016 |
1 |01/04/2016 |01/09/2016 |01/09/2016 |01/20/2016 |
This is my attempt at the code:
SELECT
t1.ID,
Prev_From_DT = t1.FROM_DT,
Prev_To_DT = t1.TO_DT,
Recent_From_DT = t2.FROM_DT,
Recent_To_DT = t2.TO_DT
FROM
tbl_Data t1
LEFT JOIN tbl_Data t2 on t1.ID = t2.ID AND t1.TO_DT <= t2.FROM_DT