Table metrics holds positive numeric measurements in value, each tagged with a segment of 'north', 'south' or 'east'. Compute each segment's coefficient of variation, STDDEV_SAMP(value) / AVG(value), which is the sample standard deviation divided by the mean. Return one row per segment with segment and cv rounded to 4 decimals.
Input Table(s)
Table metrics:
| Column | Type | Meaning |
|---|
segment | VARCHAR(10) | 'north', 'south' or 'east' |
value | DECIMAL(10,4) | A positive measurement |
The table has no primary key. Each row is one observation, and the same segment and value can appear in more than one row.
Output Contract
-
Columns, in this order:
segment
,
cv
.
-
One row for each segment that appears in
metrics
.
-
cv
is
STDDEV_SAMP(value) / AVG(value)
over that segment's rows, rounded to 4 decimal places.
-
Rows sorted by
segment
ascending.
Example
Input metrics:
| segment | value |
|---|
| east | 41.6 |
| east | 32.1 |
| east | 35.4 |
| north | 26.2 |
| north | 28.1 |
| north | 22.3 |
| south | 56.4 |
| south | 51.1 |
| east | 60.3 |
| north | 28.1 |
| south | 42.2 |
| south | 61.6 |
| south | 47.7 |
Expected output:
| segment | cv |
|---|
| east | 0.2975 |
| north | 0.1045 |
| south | 0.1453 |
East has four values with mean 42.35 and sample standard deviation about 12.598, so its coefficient of variation is about 0.29747, which rounds to 0.2975.
Constraints and Clarifications
-
value
is never NULL and is always greater than 0, so no segment has a mean of zero.
-
Duplicate rows are separate observations and all of them count. North has 28.1 twice in the example.
-
A segment with only one row has no sample standard deviation, so its
cv
is NULL.
-
A segment with no rows does not appear in the output.