Each row of emails is one user action on an email campaign. For each campaign_id, compute click_through_rate: the percentage of unique users who 'Clicked' out of the unique users who were 'Sent' the email, rounded to 2 decimal places. Order the result by campaign_id ascending.
Input Table(s)
emails
| column | type | meaning |
|---|
id | INTEGER | Unique id of the action row. Primary key. |
user_id | INTEGER | User the action belongs to. Not null. |
campaign_id | INTEGER | Campaign the email belongs to. Not null. |
action | VARCHAR(10) | One of 'Sent', 'Opened', 'Clicked'. Not null. |
action_date | DATE | Date of the action. Not null. |
Key: id is the primary key. The same user can have several rows for the same campaign and action.
Output Contract
-
Columns, in this order:
campaign_id
,
click_through_rate
.
-
One row per
campaign_id
present in
emails
.
-
click_through_rate
is 100 times the number of distinct users with a
'Clicked'
row for the campaign, divided by the number of distinct users with a
'Sent'
row for the campaign, rounded to 2 decimal places. A campaign with no clicks has a rate of 0.00.
-
Sort by
campaign_id
ascending.
Example
emails
| id | user_id | campaign_id | action | action_date |
|---|
| 1 | 1 | 1 | Sent | 2024-05-01 |
| 2 | 2 | 1 | Sent | 2024-05-01 |
| 3 | 3 | 1 | Sent | 2024-05-01 |
| 4 | 1 | 1 | Opened | 2024-05-02 |
| 5 | 1 | 1 | Clicked | 2024-05-02 |
| 6 | 1 | 1 | Clicked | 2024-05-03 |
| 7 | 2 | 1 | Opened | 2024-05-03 |
| 8 | 4 | 2 | Sent | 2024-05-04 |
| 9 | 4 | 2 | Sent | 2024-05-06 |
| 10 | 5 | 2 | Sent | 2024-05-04 |
| 11 | 4 | 2 | Clicked | 2024-05-07 |
| 12 | 6 | 3 | Sent | 2024-05-05 |
| 13 | 7 | 3 | Sent | 2024-05-05 |
| 14 | 6 | 3 | Opened | 2024-05-06 |
Expected result:
| campaign_id | click_through_rate |
|---|
| 1 | 33.33 |
| 2 | 50.00 |
| 3 | 0.00 |
Campaign 1 was sent to users 1, 2 and 3, and only user 1 clicked (twice), so the rate is 1 out of 3, or 33.33. Campaign 2 was sent to users 4 and 5; user 4 received it twice but counts once, and user 4 clicked, giving 1 out of 2, or 50.00. Campaign 3 has opens but no clicks, so its rate is 0.00.
Constraints and Clarifications
-
emails
has 1 to 10,000 rows.
user_id
is between 1 and 1000 and
campaign_id
is between 1 and 100.
-
Every
campaign_id
has at least one
'Sent'
row, so the denominator is never zero.
-
No column contains NULL, and
action
always holds one of the three exact strings shown.
-
Users are counted once per campaign and action, however many rows they have.
-
'Opened'
rows and
action_date
do not affect the rate. A click counts whether or not the user has an
'Opened'
row.
-
The numerator counts every distinct user with a
'Clicked'
row for the campaign.