I've the following dataset.
date | orig | dest | value |
---|---|---|---|
11-4 | S8 | B9 | -42 |
10-30 | S8 | B9 | -2999 |
10-15 | S8 | B9 | 959 |
10-14 | S8 | B9 | 480 |
10-13 | S8 | B9 | 0 |
10-9 | S8 | B9 | 503 |
How do I convert it into something like this?
date | orig | dest | value | new value |
---|---|---|---|---|
11-4 | S8 | B9 | -42 | 959 |
10-30 | S8 | B9 | -2999 | 959 |
10-15 | S8 | B9 | 959 | 959 |
10-14 | S8 | B9 | 480 | 480 |
10-13 | S8 | B9 | 0 | 503 |
10-9 | S8 | B9 | 503 | 503 |
This is what I've using and is incorrect:
CASE WHEN value <= 0
THEN last_value(value) over (PARTITION BY orig, dest ORDER BY date DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
ELSE value
END as new_value
The goal is here to not to replace the <=0 values with the maximum recent value but rather with the latest non-negative recent value.