Table ab_events has one row per visitor with variant ('control' or 'treatment') and outcome ('converted' or 'not_converted'). For each variant, return variant, prop_converted and prop_not_converted (the conditional distribution of outcome within the variant), each rounded to 4 decimals. The two proportions in a row must sum to 1.
Input Table(s)
ab_events
| Column | Type | Meaning |
|---|
| variant | VARCHAR(20) | Experiment group of the visitor: 'control' or 'treatment' |
| outcome | VARCHAR(20) | Result for the visitor: 'converted' or 'not_converted' |
The table has no key column. Each row is one visitor.
Output Contract
-
Columns, in this order:
variant
,
prop_converted
,
prop_not_converted
.
-
prop_converted
is the number of the variant's rows with
outcome = 'converted'
divided by the variant's total row count.
prop_not_converted
is the same for
'not_converted'
.
-
Each proportion is rounded to 4 decimals on its own, with PostgreSQL
ROUND
on a numeric value.
-
One row per variant that appears in
ab_events
, ordered by
variant
ascending.
Example
Input ab_events:
| variant | outcome |
|---|
| control | not_converted |
| treatment | converted |
| control | converted |
| control | not_converted |
| control | converted |
| control | not_converted |
| treatment | not_converted |
| treatment | not_converted |
| treatment | converted |
| treatment | not_converted |
| control | not_converted |
| treatment | converted |
| control | converted |
| treatment | converted |
Expected output:
| variant | prop_converted | prop_not_converted |
|---|
| control | 0.4286 | 0.5714 |
| treatment | 0.5714 | 0.4286 |
Each variant has 7 visitors. Control converts 3 of 7 (0.428571...) and treatment converts 4 of 7 (0.571428...).
Constraints and Clarifications
-
variant
and
outcome
are never NULL, and the values are lowercase exactly as listed, so every row counts toward exactly one of the two proportions.
-
Every variant in the output has at least one row, so the denominator is never zero.
-
The two proportions are complementary: before rounding they sum to exactly 1.
-
Duplicate rows are separate visitors and each one counts.