Your question is SQL Anomaly Spikes Query. Start with the requirements and the one table on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Given a table of high-frequency sensor logs, write a query to identify anomalous spikes using ranking and partitioning functions.
Treat a spike as a reading among the two highest distinct values for its sensor. Exclude readings with missing values.
sensor_id, log_id, recorded_at, reading_value, and spike_rank.sensor_id, spike_rank, recorded_at, and log_id.| Column | Type | Description |
|---|---|---|
| log_idPK | INT | Unique sensor log identifier |
| sensor_id | VARCHAR(30) | Sensor that produced the reading |
| recorded_at | TIMESTAMPTZ | Timestamp when the reading was recorded |
| reading_value | NUMERIC(10,2) | Measured sensor value |