Currently I have a very big table that have a snapshot of data for each month.
| ID | other | Team | Period |
|---|---|---|---|
| 1 | ..... | A | 2020-04-30 |
| 1 | ..... | A | 2020-05-31 |
| 1 | ..... | A | 2020-06-30 |
| 1 | ..... | A | 2020-07-31 |
| 1 | ..... | B | 2020-08-31 |
| 1 | ..... | B | 2020-09-30 |
| 1 | ..... | C | 2020-10-31 |
| 1 | ..... | C | 2020-11-30 |
| 1 | ..... | C | 2020-12-31 |
I would like to implement SCD on this table based on comparison of all listed columns. How Can I achieve it?
In this example I would like to have only 3 rows where Team column was changed and proper valid_from, valid_to columns based on period column.
If you can get those who built the monthly snapshot to do otherwise, encourage them strongly to do so.
For the rest: in one query, get the previous value for
team, asprev_team- I do that in a WITH clause, and name the Common table Expressionw_prev.Then I run an outer query on
w_prev, filtering by the previousteambeing NULL or different from the currentteamvalue - and use the three-parameter variant of theLEAD()OLAP function to get the next date, or, if missing, a maximum possible date: