Table readings records a numeric value with a grp label ('A', 'B' or 'C') and a reading_date. For each group, compute the per-calendar-month sample variance of value and find the largest month-over-month variance decline across all groups.
Requirements:
-
Compute each month's variance with
VAR_SAMP
.
-
Get the previous month's value with a window function ordered by month.
-
decline
= previous_variance - current_variance.
-
Return one row:
grp
and
decline
(rounded to 4 decimals) for the largest
decline
.
Input Table(s)
Table readings:
| Column | Type | Meaning |
|---|
grp | VARCHAR(5) | Group label: 'A', 'B' or 'C' |
reading_date | DATE | Date of the reading |
value | DECIMAL(10,4) | Measured value |
There is no key. Each row is one reading, and two identical rows are two separate readings.
Output Contract
At most one row with these columns, in this order:
| Column | Type | Meaning |
|---|
grp | VARCHAR | Group with the largest decline |
decline | NUMERIC | Previous month's sample variance minus this month's, rounded to 4 decimal places |
Rank candidates by decline descending, then by grp ascending, and return the first.
Example
Input readings:
| grp | reading_date | value |
|---|
| B | 2024-01-12 | 44.9 |
| A | 2024-03-12 | 58.5 |
| A | 2024-02-12 | 55.1 |
| A | 2024-02-12 | 57.4 |
| A | 2024-01-12 | 39.3 |
| B | 2024-01-12 | 50.6 |
| B | 2024-03-12 | 46 |
| A | 2024-03-12 | 12.3 |
| C | 2024-02-12 | 16.9 |
| C | 2024-03-12 | 26.3 |
| C | 2024-02-12 | 54.8 |
| B | 2024-02-12 | 14.9 |
| C | 2024-03-12 | 39.3 |
| C | 2024-01-12 | 20.6 |
| C | 2024-01-12 | 14.7 |
| A | 2024-03-12 | 15.4 |
| A | 2024-03-12 | 11.7 |
| B | 2024-01-12 | 30.4 |
| B | 2024-02-12 | 43 |
| A | 2024-03-12 | 22.1 |
| A | 2024-01-12 | 28.8 |
| C | 2024-02-12 | 24.9 |
| C | 2024-02-12 | 48 |
| C | 2024-02-12 | 35.5 |
| B | 2024-03-12 | 59.7 |
| A | 2024-01-12 | 40.7 |
| C | 2024-03-12 | 33.9 |
| A | 2024-03-12 | 12.2 |
| B | 2024-01-12 | 32.3 |
| B | 2024-01-12 | 12.6 |
| C | 2024-02-12 | 31.1 |
Output:
Group B's sample variance is 394.805 in February 2024 (readings 14.9 and 43) and 93.845 in March 2024 (readings 46 and 59.7), a decline of 300.96. The next largest decline is group C from February to March, about 158.5227.
Constraints and Clarifications
-
A month is a calendar month of
reading_date
, year included, so January 2024 and January 2025 are different months.
-
A decline compares a month with the immediately preceding calendar month of the same group. If that preceding month has no variance for the group, the month has no decline.
-
A month with a single reading has no sample variance (
VAR_SAMP
returns NULL), so it neither has a decline nor serves as the previous month for one.
-
value
is never NULL. Identical rows are separate readings and all count.
-
The largest decline can be negative if no group's variance fell; return it anyway. If no group has two consecutive months with a variance, return no rows.
-
If several groups share the largest decline after rounding to 4 decimals, return the one that comes first alphabetically.