Background: I have a table like the below where I'm trying to get the median value
based on a rolling 30 day lookback from each created_date
partitioned by each city
.
The problem is that I have missing dates for some cities so bound by rows preceding
won't work in this situation.
Note: I do have a date-spine
table that I can leverage, but not sure if it's helpful here
CREATED_DATE | CITY | VALUE |
---|---|---|
2018-08-30 | Charlotte | 374900 |
2018-08-31 | Charlotte | 272000 |
2018-09-10 | Charlotte | 1 |
2018-09-24 | Charlotte | 365000 |
2018-10-04 | Charlotte | 342000 |
2018-10-07 | Charlotte | 460000 |
2018-10-08 | Charlotte | 91000 |
2018-10-15 | Charlotte | 342000 |
2018-10-18 | Charlotte | 155000 |
2018-10-19 | Charlotte | 222000 |
... | ... | ... |
Expected Output:
CREATED_DATE | CITY | VALUE | MOVING_MEDIAN_30_DAY |
---|---|---|---|
2018-08-30 | Charlotte | 374900 | 374900 |
2018-08-31 | Charlotte | 272000 | 323450 |
2018-09-10 | Charlotte | 1 | 272000 |
2018-09-24 | Charlotte | 365000 | 318500 |
2018-10-04 | Charlotte | 342000 | 342000 |
2018-10-07 | Charlotte | 460000 | 353500 |
2018-10-08 | Charlotte | 91000 | 342000 |
2018-10-15 | Charlotte | 342000 | 342000 |
2018-10-18 | Charlotte | 155000 | 342000 |
2018-10-19 | Charlotte | 222000 | 342000 |
... | ... | ... | ... |
Question: How can I get to my expected output using sql / snowflake?
Thanks in advance!!