Forward-fill a sparse daily sensor series over every calendar day in its range

Quick Overview

Write a SQL query that lists every calendar day between the first and last sensor reading and fills each missing day with the most recent earlier value. It tests building a date spine, outer joins, and carrying values forward across runs of missing days using window functions.

Forward-fill a sparse daily sensor series over every calendar day in its range

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: hard

Interview Round: Other

`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`. ```hint Runs of missing days Several missing days in a row must all find the same earlier reading. Check that your approach gives both 2024-01-04 and 2024-01-05 the value 20 in the example. ``` ### 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.

Overview: Write a SQL query that lists every calendar day between the first and last sensor reading and fills each missing day with the most recent earlier value. It tests building a date spine, outer joins, and carrying values forward across runs of missing days using window functions.

PracHub logo
General
Nov 25, 2026
hardData ScientistOtherData Manipulation (SQL/Python)
0
0

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

ColumnTypeMeaning
reading_dateDATEDay of the reading, unique
reading_valueINTValue 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_datereading_value
2024-01-0110
2024-01-0320
2024-01-0650

Expected output:

reading_datefilled_value
2024-01-0110
2024-01-0210
2024-01-0320
2024-01-0420
2024-01-0520
2024-01-0650

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.