Skip to content
Advertisement

Valid_from Valid_to from a full loaded table

There is a source table which loads the data full and monthly. The table looks like below example.

Source table:

pk code_paym code_terms etl_id
1 2 3 2020-08-01
1 2 3 2020-09-01
1 2 4 2020-10-01
1 2 4 2020-11-01
1 2 4 2020-12-01
1 2 4 2021-01-01
1 2 3 2021-02-01
1 2 3 2021-03-01
1 2 3 2021-04-01
1 2 3 2021-05-01

I would like to create valid_from valid_to columns from the source table like below example.

Desired Output:

pk code_paym code_terms valid_from valid_to
1 2 3 2020-08-01 2020-09-01
1 2 4 2020-10-01 2021-01-01
1 2 3 2021-02-01 2021-05-01

As it can be seen attributes can go back to the same values by the time. How can I make this output happen by sql code?

Thank you very much, Regards

Advertisement

Answer

Using CONDITIONAL_TRUE_EVENT windowed function to determine continuous subgroups:

Query:

Output:

enter image description here

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement