Campaign by day fact table with cumulative spend
The task
Marketing wants a daily table at the grain one row per campaign per day it was live, for 2025-03-10 to 2025-03-16 inclusive.
- A campaign is live on a day when
started_on <= dayandended_onisNULLorended_on >= day(ended_onis the last day live). ad_dailyhas no row for a day with no delivery. A live day with no row getsimpressions,clicksandspendof 0.cumulative_spendis the totalspendof the campaign from its first day up to and including that day, counting days before 2025-03-10 too.
Return campaign_id, day, impressions, clicks, spend and cumulative_spend. Sort by campaign_id, then day.
Expected output
campaign_iddayimpressionsclicksspendcumulative_spend- Sorted by campaign_id ascending, then day ascending.
- Checked on the sample and on 2 hidden datasets. How grading works
Tables Marketing team · click a name to insert it
campaigns10 rows in the sample
One row per campaign. Key: campaign_id.
| int | Unique id. | |
| text | Name. | |
| text | search, social, video, email or affiliate. | |
| date | First day live. | |
| date | Last day live. NULL if still running. | |
| numeric(10,2) | Budget per day, USD. |
ad_daily969 rows in the sample
One row per campaign per day it served ads. Days with no delivery have no row. Key: campaign_id, day.
| int | The campaign. | |
| date | The day. | |
| int | Times an ad was shown. | |
| int | Clicks. | |
| numeric(10,2) | Money spent, USD. |
signups2,108 rows in the sample
One row per new user. campaign_id is the campaign credited with the sign-up. Key: user_id.
| int | Unique id. | |
| timestamp | When they signed up (UTC). | |
| int | Credited campaign. NULL for organic sign-ups. | |
| text | Two-letter country code. |
purchases2,294 rows in the sample
One row per paid order by a user. Key: purchase_id.
| int | Unique id. | |
| int | Buyer. | |
| timestamp | When (UTC). | |
| numeric(10,2) | Order value, USD. |
Related questions
- Daily end-of-day balance from transactions with a date spinehard
- Salary validity ranges built with LEADmedium
- Active accounts and seats per day from subscription datesmedium
- Agent by month fact table of tickets created and resolvedmedium
- Daily stock levels with a date spine and carry forwardhard
- Grain check for a user by day purchase modeleasy
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL