sensor_readings has readings on some days only. Generate every calendar day from MIN(reading_date) to MAX(reading_date) inclusive, and forward-fill missing days with the most recent preceding reading_value. Return one row per day with reading_date and filled_value, which is never NULL.
Input Table(s)
sensor_readings
| Column | Type | Meaning |
|---|
| reading_date | DATE | Day of the reading, unique |
| reading_value | INT | Value recorded that day |
Key: reading_date is unique, so there is at most one reading per day.
Output Contract
-
Columns, in this order:
reading_date
,
filled_value
.
-
One row for every calendar day from the earliest to the latest
reading_date
, inclusive, with no gaps and no repeated days.
-
filled_value
is the day's own
reading_value
when a reading exists, and otherwise the
reading_value
of the latest earlier day that has one.
-
Order by
reading_date
ascending.
Example
Input sensor_readings:
| reading_date | reading_value |
|---|
| 2024-01-01 | 10 |
| 2024-01-03 | 20 |
| 2024-01-06 | 50 |
Expected output:
| reading_date | filled_value |
|---|
| 2024-01-01 | 10 |
| 2024-01-02 | 10 |
| 2024-01-03 | 20 |
| 2024-01-04 | 20 |
| 2024-01-05 | 20 |
| 2024-01-06 | 50 |
The output runs from 2024-01-01 to 2024-01-06. January 2 inherits 10 from January 1, January 4 and 5 inherit 20 from January 3, and January 6 has its own reading of 50.
Constraints and Clarifications
-
sensor_readings
has between 1 and 1,000 rows.
-
reading_date
values are unique calendar days.
-
reading_value
is a non-negative integer and is never
NULL
.
-
The first day of the range always has a reading, so every day has a value to inherit and
filled_value
is never
NULL
.
-
With a single row, the output is that one day.