I have the sample data below and I am looking to populate the value in the expectedOutput column. I want it to be calculated according to the rule -- if isValid = 1, give me the fruit value the last time isValid = 1. So in this example, row 1 is null because that is the first value. Row 6 shows apple because this row is valid and apple was the fruit the last time there was a valid value.
row fruit isValid expectedOutput (Prior valid value)
1 apple 1 NULL
2 apple 0 NULL
3 apple 0 NULL
4 apple 0 NULL
5 orange 0 NULL
6 orange 1 apple
7 grape 1 orange
8 grape 0 NULL
Here's what I've tried, doesn't quite get me the right result.
LAST_VALUE(case when isValid = 1 then fruit end IGNORE NULLS) OVER(PARTITION BY all ORDER BY row)
