Imputing Missing Values in Sequences
In this lesson, you will learn how to intelligently fill gaps in a sequence by substituting missing values with the average of neighboring or historical data points.
The Problem of Sparse Sequences
When analyzing time series or sequential data, gaps are common. Perhaps a sensor failed to record a temperature, or a sales report wasn't submitted for a specific day. If you calculate an average over that period, a NULL value might skew your results or break your logic. Instead of just skipping these gaps, we can "impute" them using the surrounding data context.
Using Window Functions for Imputation
To fill these gaps, we rely on AVG() as a window function. By looking at a frame of rows before and after the missing point, we can calculate a "smoothed" estimate.
Imagine we have a table sensor_data with a reading column. Some rows are NULL. We can use the IGNORE NULLS clause (available in many modern SQL dialects) or a simple COALESCE with a windowed average:
SELECT
timestamp,
reading,
AVG(reading) OVER (
ORDER BY timestamp
ROWS BETWEEN 3 PRECEDING AND 3 FOLLOWING
) as imputed_reading
FROM sensor_data;
In this example, for any missing reading, the query calculates the mean of the 6 surrounding data points. If you want a more historical approach, you could adjust the window to look only at PRECEDING rows, effectively carrying the local trend forward.
Why Context Matters
The choice of window size is critical:
- Narrow windows: Capture local fluctuations accurately but are sensitive to outliers.
- Wide windows: Create a smoother trend but might ignore sudden shifts in the data sequence.
By combining this with the COALESCE function, you can create a "cleaned" version of your dataset:
SELECT
timestamp,
COALESCE(reading, smoothed_reading) as final_reading
FROM (
SELECT timestamp, reading,
AVG(reading) OVER (ORDER BY timestamp ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) as smoothed_reading
FROM sensor_data
) as subquery;
This pattern ensures your downstream reports remain complete, preventing the "vanishing data" problem that occurs when NULL values are treated as zero or ignored entirely by aggregation functions.
Key Takeaway: You can replace missing values by using window-based averages to interpolate data points based on their immediate chronological neighbors.