Write a SQL query using window functions to compare each row with the previous row in the same partition.
Use the observations table. Within each partition_key, rows are ordered by observed_at and then observation_id.
partition_key, observation_id, observed_at, metric_value, previous_metric_value, and difference_from_previous.NULL when no previous row exists or either value needed for subtraction is NULL.partition_key with NULL partitions last, then observed_at, and observation_id.| Column | Type | Description |
|---|---|---|
| observation_idPK | INT | Unique observation identifier |
| partition_key | VARCHAR(50) | Partition to which the observation belongs |
| observed_at | TIMESTAMP | Timestamp used to order observations |
| metric_value | DECIMAL(10,2) | Observed numeric metric |