The assignments table holds one row per user in an A/B test. Each user is in either the control or the treatment group, and converted is 1 if the user converted and 0 otherwise. Return one row per group with group_name, users (the number of rows in the group), conversions (the sum of converted) and conversion_rate (conversions / users, rounded to 4 decimals).
Input Table(s)
Table assignments:
| Column | Type | Meaning |
|---|
group_name | VARCHAR(20) | Experiment arm, either 'control' or 'treatment' |
converted | SMALLINT | 1 if the user converted, 0 if not |
The table has no key column. Each row is one user.
Output Contract
One row per group, with these columns in this order:
| Column | Type | Rule |
|---|
group_name | VARCHAR | The group |
users | BIGINT | Number of rows in the group |
conversions | BIGINT | Sum of converted in the group |
conversion_rate | NUMERIC | conversions / users, rounded to 4 decimal places |
Order the rows by group_name ascending. Each group appears once, so that order is unique.
Example
Input assignments:
| group_name | converted |
|---|
| treatment | 1 |
| treatment | 1 |
| control | 0 |
| control | 1 |
| control | 1 |
| control | 1 |
| treatment | 1 |
| treatment | 0 |
| treatment | 0 |
| treatment | 1 |
| control | 0 |
| control | 0 |
| control | 0 |
| treatment | 1 |
| control | 1 |
| control | 1 |
| treatment | 1 |
| treatment | 0 |
| treatment | 0 |
| treatment | 1 |
| control | 1 |
| control | 0 |
Expected output:
| group_name | users | conversions | conversion_rate |
|---|
| control | 11 | 6 | 0.5455 |
| treatment | 11 | 7 | 0.6364 |
Control has 11 rows and 6 of them converted, and 6/11 = 0.54545... rounds to 0.5455. Treatment has 11 rows with 7 conversions, and 7/11 = 0.63636... rounds to 0.6364.
Constraints and Clarifications
-
group_name
only takes the values
'control'
and
'treatment'
, and both appear at least once.
-
converted
is always 0 or 1 and never NULL.
-
There is no user id, so every row counts as one user. Do not deduplicate identical rows.
-
Rounding follows PostgreSQL's
ROUND
on NUMERIC, which rounds halves away from zero.