I have the below table extracted from a Type 2 Slowly Changing dimension. I want to group rows to only have distinct classifications and their respective 'switch' time periods in a chronological order. A simple GROUP BY won't work because of the classifications that can 'come back', like the classification 'K' that 'came back' in RowNum 5.
Current Data :
RowNum_Helper | Item_ID | Classification | Start_Date | End_Date |
---|---|---|---|---|
1 | X | K | 2021-01-07 02:02:44.060 | 2021-01-08 02:03:45.980 |
2 | X | K | 2021-01-08 02:03:45.980 | 2021-01-31 20:03:38.993 |
3 | X | N | 2021-01-31 20:03:38.993 | 2021-03-26 14:44:08.617 |
4 | X | N | 2021-03-26 14:44:08.617 | 2021-04-08 02:02:51.013 |
5 | X | K | 2021-04-08 02:02:51.013 | 2021-10-13 02:02:51.013 |
6 | X | N | 2021-10-13 02:02:51.013 | 2021-10-14 02:02:51.013 |
7 | X | K | 2021-10-14 02:02:51.013 | 9999-12-31 23:59:59.999 |
8 | A | K | 2021-06-14 02:02:51.013 | 9999-12-31 23:59:59.999 |
Desired data :
Item_ID | Classification | Earliest_Start_Date | Latest_End_Date |
---|---|---|---|
X | K | 2021-01-07 02:02:44.060 | 2021-01-31 20:03:38.993 |
X | N | 2021-01-31 20:03:38.993 | 2021-04-08 02:02:51.013 |
X | K | 2021-04-08 02:02:51.013 | 2021-10-13 02:02:51.013 |
X | N | 2021-10-13 02:02:51.013 | 2021-10-14 02:02:51.013 |
X | K | 2021-10-14 02:02:51.013 | 9999-12-31 23:59:59.999 |
A | K | 2021-06-14 02:02:51.013 | 9999-12-31 23:59:59.999 |
I tired using windowed functions like SUM OVER PARTITION BY etc, but can't really find an elegant way of doing it. Any help would be appreciated.
SQLFiddle seems to be offline, I used StackExchangeDE to create table with values for playground