The metrics table records a numeric value per user with a grp label of 'A' or 'B'. Using conditional aggregation, return one row with variance_a and variance_b: the sample variance (VAR_SAMP) of value for each group, rounded to 4 decimals.
Input Table(s)
metrics
| column | type | meaning |
|---|
| grp | VARCHAR(5) | Experiment group label, 'A' or 'B' |
| value | DECIMAL(10,4) | The user's measured metric |
The table has no declared key. Each row is one user's measurement.
Output Contract
-
Exactly one row with two columns, in this order:
variance_a
,
variance_b
.
-
Each column is the sample variance (denominator
n−1
) of
value
for that group, rounded to 4 decimal places.
-
The result has a single row, so no ordering is needed.
Example
Input metrics:
| grp | value |
|---|
| A | 10.7 |
| B | 38.2 |
| A | 17.5 |
| A | 14.1 |
| B | 23.9 |
| B | 18.4 |
| A | 19.8 |
| B | 5.1 |
Expected output:
| variance_a | variance_b |
|---|
| 15.8292 | 187.7267 |
Group A has mean 15.525 and squared deviations summing to 47.4875, so its sample variance is 47.4875 / 3, about 15.8292. Group B has mean 21.4 and squared deviations summing to 563.18, so its sample variance is 563.18 / 3, about 187.7267.
Constraints and Clarifications
-
Rows with a NULL
value
are ignored.
-
A group with fewer than two non-NULL values has no sample variance, so its column is NULL.
-
Rows with any
grp
label other than
'A'
or
'B'
do not count toward either column.
-
Duplicate rows are separate observations and all count.
-
If the table is empty, the result is still one row, with both columns NULL.