Table Schema
Inspect
Table
| Column | Type |
|---|---|
| ad_id | integer |
| campaign_id | varchar |
| revenue | decimal |
Calculate total revenue per ad campaign.
Table Schema
| Column | Type |
|---|---|
| ad_id | integer |
| campaign_id | varchar |
| revenue | decimal |
Sample Data
| ad_id | campaign_id | revenue |
|---|---|---|
| 1 | C1 | 100 |
| 2 | C1 | 200 |
| 3 | C2 | 300 |
| 4 | C2 | 150 |
| 5 | C3 | 400 |
| campaign_id | total_revenue |
|---|---|
| C1 | 300 |
| C2 | 450 |
| C3 | 400 |
SQL Editor
| campaign_id | total_revenue |
|---|---|
| C1 | 300 |
| C2 | 450 |
| C3 | 400 |
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 campaign_id, SUM(revenue) AS total_revenue FROM ads GROUP BY campaign_id;
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