I got this SCD table:
start_date | end_date | partition |
---|---|---|
2022-03-08 15:35:09.856 | 2022-03-09 14:57:36.610 | 1 |
2022-03-09 14:57:36.610 | 2022-05-18 13:26:31.195 | 2 |
2022-05-18 13:26:31.195 | 2022-08-02 10:12:02.441 | 2 |
2022-08-02 10:12:02.441 | 2022-09-01 11:10:01.019 | 2 |
2022-09-01 11:10:01.019 | 2022-09-01 11:10:20.777 | 1 |
2022-09-01 11:10:20.777 | 2022-09-01 11:21:26.526 | 1 |
I would like to know for each partition the last value of start_date and end_date of the other partition (there are only two). for the given table:
start_date | end_date | partition | max_start_date | max_end_date |
---|---|---|---|---|
2022-03-08 15:35:09.856 | 2022-03-09 14:57:36.610 | 1 | null | null |
2022-03-09 14:57:36.610 | 2022-05-18 13:26:31.195 | 2 | 2022-03-08 15:35:09.856 | 2022-03-09 14:57:36.610 |
2022-05-18 13:26:31.195 | 2022-08-02 10:12:02.441 | 2 | 2022-03-08 15:35:09.856 | 2022-03-09 14:57:36.610 |
2022-08-02 10:12:02.441 | 2022-09-01 11:10:01.019 | 2 | 2022-03-08 15:35:09.856 | 2022-03-09 14:57:36.610 |
2022-09-01 11:10:01.019 | 2022-09-01 11:10:20.777 | 1 | 2022-08-02 10:12:02.441 | 2022-09-01 11:10:01.019 |
2022-09-01 11:10:20.777 | 2022-09-01 11:21:26.526 | 1 | 2022-08-02 10:12:02.441 | 2022-09-01 11:10:01.019 |
I tried some last_value window function and didn’t made it. like this:
, last_value (start_date) OVER (partition by partition = ‘1’ order by start_date asc) as last_start_date_partition
, last_value (end_date) OVER (partition by partition = ‘1’ order by end_date asc) as last_end_date_partition
is there any option to inject a condition to window function and make it function that way?
3
Answers
Using
dense_rank
:See fiddle.
Using windows functions and gaps-and-islandish approach:
Fiddle
You could do a self-left join and aggregate as the following:
See demo