I have a table ordered by ID with column Value.
| ID | Value |
| -------- | -------------- |
| 1 | 50 |
| 2 | 50 |
| 3 | 62 |
| 4 | 62 |
| 5 | 62 |
| 6 | 79 |
| 7 | 90 |
| 8 | 90 |
I would like to create another column Prev_Value that for each row of column Value takes the previous/preceding number that differs from the current row value, as in the table below.
Output table:
| ID | Value |Prev_Value |
| -------- | -------------- |---------------|
| 1 | 50 |NULL |
| 2 | 50 |NULL |
| 3 | 62 |50 |
| 4 | 62 |50 |
| 5 | 62 |50 |
| 6 | 79 |62 |
| 7 | 90 |79 |
| 8 | 90 |79 |
Should I use modified LAG() function, the CROSS APPLY or nested CASE and what approach would be the most time-efficient? Any help would be appreciated.
Here are some references that unfortunately does not solve my problem: LAG(offset) until value is reached in BigQuery and SQL Server : select distinct until the value is changed