SQL SUM
Adds up every value in a column into a single total.
What & Why
SUM(column) adds every value in that column together, across all rows in the result.
See How It Works
BUSINESS QUESTION
Total campaign spend across the campaign portfolio.
| id | name | channel | spend | start_date | end_date | status | target_segment |
|---|---|---|---|---|---|---|---|
| 1 | Spring Launch | google_ads | 55000.00 | 2024-01-15 | 2024-03-31 | active | smb |
| 2 | Retention Webinar | 45000.00 | 2024-02-10 | 2024-04-15 | active | enterprise | |
| 3 | Finance Retargeting | 50000.00 | 2024-03-12 | 2024-05-31 | active | enterprise | |
| 4 | Enterprise Search | google_ads | 60000.00 | 2024-04-01 | 2024-06-30 | active | enterprise |
Watch the aggregate accumulateRow 1 of 4
SUM(spend)
| row | campaign | channel | spend | status | running sum |
|---|---|---|---|---|---|
| 1 | Spring Launch | google_ads | 55,000 | ADD | 55,000 |
| 2 | Retention Webinar | 45,000 | WAIT | — | |
| 3 | Finance Retargeting | 50,000 | WAIT | — | |
| 4 | Enterprise Search | google_ads | 60,000 | WAIT | — |
Running result
SUM(spend) = 55,00055,000 is added; the running sum becomes 55,000.
EXAMPLE QUERY
SELECT SUM(spend) AS total_campaign_spend
FROM marketing.campaigns;Now You Try
Practice this concept
Marketing wants total email recipients by campaign.
Available schema
marketingPrefix tables with marketing.table_name.
campaign_idtotal_recipientsmarketing.email_sends| Column | Type |
|---|---|
| id | integer |
| campaign_id | integer |
| sent_at | timestamp with time zone |
| recipients | integer |
| opens | integer |
| clicks | integer |
| unsubscribes | integer |
| bounces | integer |
| legacy_send_group | text |
query.sql
Sign up free to try it on a real business scenario