The scores table holds one score per row for cohort 'A' or 'B', and the single-row params table supplies one threshold. For each cohort, use CUME_DIST() over that cohort's scores to find the fraction of its scores that are less than or equal to threshold. Return one row per cohort with cohort and threshold_percentile rounded to 4 decimals.
Input Table(s)
scores
| Column | Type | Meaning |
|---|
| cohort | VARCHAR(5) | Cohort label, 'A' or 'B' |
| score | DECIMAL(8,1) | One observed score |
scores has no primary key. Each row is one observation, and two rows may have the same cohort and score.
params
| Column | Type | Meaning |
|---|
| threshold | DECIMAL(8,1) | The value whose percentile is wanted |
params has exactly one row.
Output Contract
Return these columns, in this order:
| Column | Meaning |
|---|
| cohort | The cohort label |
| threshold_percentile | Fraction of the cohort's scores that are less than or equal to threshold, rounded to 4 decimal places |
Return one row for each cohort that has at least one row in scores, ordered by cohort ascending.
Example
Example 1. Input scores:
| cohort | score |
|---|
| B | 72.0 |
| B | 65.3 |
| A | 46.0 |
| A | 20.6 |
| A | 37.2 |
| B | 64.6 |
| B | 52.3 |
| A | 64.4 |
| B | 72.6 |
| A | 34.1 |
| A | 56.4 |
| B | 39.3 |
| A | 58.9 |
Input params:
Expected output:
| cohort | threshold_percentile |
|---|
| A | 0.5714 |
| B | 0.1667 |
Cohort A has 7 scores, and 4 of them (20.6, 34.1, 37.2 and 46.0) are at or below 50, so the result is 4 / 7 = 0.5714. Cohort B has 6 scores and only 39.3 qualifies, so the result is 1 / 6 = 0.1667.
Example 2. Input scores:
| cohort | score |
|---|
| A | 10.0 |
| A | 10.0 |
| A | 12.5 |
| A | 8.0 |
| B | 15.0 |
| B | 11.0 |
| B | 20.0 |
Input params:
Expected output:
| cohort | threshold_percentile |
|---|
| A | 0.7500 |
| B | 0.0000 |
In cohort A, both rows equal to the threshold count, along with 8.0, giving 3 / 4. No score in cohort B is at or below 10, so its result is 0.
Constraints and Clarifications
-
cohort
and
score
are never NULL, and
threshold
is not NULL.
-
A score equal to
threshold
counts as at or below it.
-
Rows with equal scores are separate observations and each one counts.
-
A cohort whose scores are all above
threshold
returns 0.
-
A cohort with no rows in
scores
does not appear in the output.