Table Schema
Inspect
Table
| Column | Type |
|---|---|
| ad_id | integer |
| impressions | integer |
| clicks | integer |
Calculate CTR per ad.
Table Schema
| Column | Type |
|---|---|
| ad_id | integer |
| impressions | integer |
| clicks | integer |
Sample Data
| ad_id | impressions | clicks |
|---|---|---|
| 1 | 1000 | 100 |
| 2 | 2000 | 150 |
| 3 | 1500 | 300 |
| ad_id | ctr |
|---|---|
| 1 | 0.1000 |
| 2 | 0.0750 |
| 3 | 0.2000 |
SQL Editor
| ad_id | ctr |
|---|---|
| 1 | 0.1000 |
| 2 | 0.0750 |
| 3 | 0.2000 |
Hints
Solution
Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.
SELECT ad_id, 1.0 * clicks / impressions AS ctr FROM ads;
Explanation
Read the expected output columns to determine the final grain.
Aggregate or rank the input rows to calculate the requested metric.
Filter, sort, and alias the final columns to match the output.
Related Questions