Skip to content

Rolling seven-day spend when days are missing

HardFull bankRunning totals, LAG and LEADDates and time18 min2 hidden datasets

The task

A pacing report shows, for campaign 1, how much it spent over the last 7 calendar days. A campaign that did not serve ads on a day has no row for that day.

For every ad_daily row of campaign 1 with a day from 2025-06-17 to 2025-06-30, return:

  • day
  • spend: that day's spend
  • spend_7d: the total spend on that day and the 6 calendar days before it (7 calendar days in all). A day with no row counts as 0.

Days before 2025-06-17 are not shown but they still count inside the 7-day windows. Only days that have a row appear in the output. Sort by day.

Expected output

dayspendspend_7d
  • Sorted by 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.

intUnique id.
textName.
textsearch, social, video, email or affiliate.
dateFirst day live.
dateLast 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.

intThe campaign.
dateThe day.
intTimes an ad was shown.
intClicks.
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.

intUnique id.
timestampWhen they signed up (UTC).
intCredited campaign. NULL for organic sign-ups.
textTwo-letter country code.
purchases2,294 rows in the sample

One row per paid order by a user. Key: purchase_id.

intUnique id.
intBuyer.
timestampWhen (UTC).
numeric(10,2)Order value, USD.

Related questions

Loading the editor and PostgreSQL