select *
from example;
edate userid status
2022-05-01 abc123 true
2022-05-02 abc123 (null)
2022-05-03 abc123 (null)
2022-05-04 abc123 (null)
2022-05-05 abc123 false
2022-05-06 abc123 (null)
2022-05-07 abc123 (null)
2022-05-08 abc123 (null)
2022-05-09 abc123 true
2022-05-10 abc123 (null)
I want to write a new field, 'status_backfilled' based on the most recent data point for a userId.
In the example data, The users status is true on May 1st, then null untill May 5th. So, I would like the new field to be true between May 1st till May 4th. Then the status switches to false. This value is unchanged till May 9th, so I want false between May 5th till 8th, then true again.
Desired output:
select *
from example_desired;
edate userid status_backfilled
2022-05-01 abc123 true
2022-05-02 abc123 true
2022-05-03 abc123 true
2022-05-04 abc123 true
2022-05-05 abc123 false
2022-05-06 abc123 false
2022-05-07 abc123 false
2022-05-08 abc123 false
2022-05-09 abc123 true
2022-05-10 abc123 true
How can I columnwise coalesce to get the most recent non null status for a user where data are sorted, in this case by date?