The events table has one row per logged event, with its variant ('A' or 'B') and event_type ('impression' or 'action'). Return one row per variant with variant, impressions (the count of 'impression' rows), actions (the count of 'action' rows), and ctr = actions / impressions rounded to 4 decimals.
Input Table(s)
events
| Column | Type | Meaning |
|---|
| variant | VARCHAR(5) | Experiment variant, 'A' or 'B' |
| event_type | VARCHAR(20) | 'impression' or 'action' |
events has no primary key. Each row is one event, and identical rows are separate events.
Output Contract
Return these columns, in this order:
| Column | Meaning |
|---|
| variant | The variant label |
| impressions | Number of 'impression' rows for the variant |
| actions | Number of 'action' rows for the variant |
| ctr | actions / impressions as a decimal, rounded to 4 decimal places, or NULL when impressions is 0 |
Return one row for each variant that has at least one row in events, ordered by variant ascending.
Example
Example 1. Input events:
| variant | event_type |
|---|
| A | impression |
| A | action |
| B | impression |
| B | impression |
| B | impression |
| A | action |
| B | action |
| B | impression |
| A | action |
| A | action |
| A | impression |
| B | impression |
| B | impression |
| A | action |
| B | impression |
| A | impression |
| A | impression |
| B | impression |
| B | impression |
| A | impression |
| A | impression |
| B | impression |
| B | impression |
Expected output:
| variant | impressions | actions | ctr |
|---|
| A | 6 | 5 | 0.8333 |
| B | 11 | 1 | 0.0909 |
Variant A has 6 impressions and 5 actions, so its CTR is 5 / 6 = 0.8333. Variant B has 11 impressions and 1 action, so its CTR is 1 / 11 = 0.0909.
Example 2. Input events:
| variant | event_type |
|---|
| A | impression |
| A | action |
| B | action |
Expected output:
| variant | impressions | actions | ctr |
|---|
| A | 1 | 1 | 1.0000 |
| B | 0 | 1 | NULL |
Variant B has no impressions, so its CTR is undefined and is returned as NULL.
Constraints and Clarifications
-
variant
and
event_type
are never NULL, and
event_type
takes only the two listed values.
-
A variant with no impressions gets
ctr
NULL, and its
impressions
is 0.
-
The CTR is a ratio of event counts and is not capped at 1.