I have a table that includes users that created an event in the app and they produced some revenue based on this event (it's added cummulatively every day):
date | user_id | revenue |
---|---|---|
2022-04-01 | A | 0.5 |
2022-04-01 | B | 0.3 |
2022-04-01 | C | 0.7 |
2022-04-02 | B | 0.6 |
2022-04-02 | C | 0.9 |
2022-04-03 | C | 1.2 |
What I want to do is use the data about all the users from the first day, but if they don't bring any revenue, I would like to use the revenue value for this user from the previous day, like so:
date | user_id | revenue |
---|---|---|
2022-04-01 | A | 0.5 |
2022-04-01 | B | 0.3 |
2022-04-01 | C | 0.7 |
2022-04-02 | A | 0.5 |
2022-04-02 | B | 0.6 |
2022-04-02 | C | 0.9 |
2022-04-03 | A | 0.5 |
2022-04-03 | B | 0.6 |
2022-04-03 | C | 1.2 |
My initial idea was to somehow copy first day user_id's and leave the revenue value for this day as null:
date | user_id | revenue |
---|---|---|
2022-04-01 | A | 0.5 |
2022-04-01 | B | 0.3 |
2022-04-01 | C | 0.7 |
2022-04-02 | A | null |
2022-04-02 | B | 0.6 |
2022-04-02 | C | 0.9 |
2022-04-03 | A | null |
2022-04-03 | B | null |
2022-04-03 | C | 1.2 |
Then I would use this to find the right values to fill NULLs with
SELECT date,
user_id,
revenue,
LAST_VALUE(revenue, IGNORE NULLS) as last_values
FROM table
So the question is, how do I go about "copying" my first day users to every following day in the table? Maybe, there is a better solution than the one I've thought about?