I have a database with accounts and historical status changes
select Date, Account, OldStatus, NewStatus from HistoricalCodes order by Account, Date
Date | Account | OldStatus | NewStatus |
---|---|---|---|
2020-01-01 | 12345 | 1 | 2 |
2020-10-01 | 12345 | 2 | 3 |
2020-11-01 | 12345 | 3 | 2 |
2020-12-01 | 12345 | 2 | 1 |
2020-01-01 | 54321 | 2 | 3 |
2020-09-01 | 54321 | 3 | 2 |
2020-12-01 | 54321 | 2 | 3 |
For every account I need to determine Start Date and End Date when Status = 2. An additional challenge is that the status can change back and forth multiple times. Is there a way in SQL to create something like this for at least first two timeframes when account was in 2? Any ideas?
Account | StartDt_1 | EndDt_1 | StartDt_2 | EndDt_2 |
---|---|---|---|---|
12345 | 2020-01-01 | 2020-10-01 | 2020-11-01 | 2020-12-01 |
54321 | 2020-09-01 | 2020-12-01 |