Explain window functions in SQL and provide a practical use case for calculating rolling averages over time-series data.
Use the table below as the input source. Write a SQL query that returns a rolling 3-day average for each device's metric value, ordered by device and timestamp.
device_id, reading_ts, metric_value, rolling_3_day_avgdevice_id, then reading_tsdevice_id and reading_ts in ascending order.NULL metric values when calculating averages and return NULL when no values exist in the frame.rolling_3_day_avg without rounding, preserving the average's full numeric precision.| Column | Type | Description |
|---|---|---|
| reading_idPK | INT | Unique reading identifier |
| device_id | VARCHAR(50) | Device identifier |
| reading_ts | TIMESTAMP | Timestamp when the reading was recorded |
| metric_value | NUMERIC(10,2) | Measured value for the reading |