Running Month-to-Date Ad Spend per Campaign

Last updated: November 29, 2026

Quick Overview

Given daily spend per ad campaign, return each day's spend alongside the campaign's month-to-date total and the share of the month-to-date total that day contributed.

Meta
Data Manipulation (SQL/Python)
Data Scientist
Meta
November 29, 2026
Data Scientist
Online Assessment
Data Manipulation (SQL/Python)
Easy

0

0

0 solved


Given daily spend per ad campaign, return each day's spend alongside the campaign's month-to-date total and the share of the month-to-date total that day contributed.

Advertiser-facing reporting in Ads Manager leans on cumulative metrics, so Meta assessments commonly include a running total question. The grader checks that the partition resets at month boundaries and that you understand the default window frame.

What the Interviewer Expects
  • Partitions by campaign and calendar month so the total resets each month
  • Orders by date inside the window to get a running sum
  • States the frame explicitly with ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
  • Computes the daily share with a guarded division
  • Mentions what happens if a campaign has two rows for the same day
Key Topics to Cover
running sum
PARTITION BY month
window frame
DATE_TRUNC
cumulative metrics
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would you add the same month-to-date figure from the previous month for comparison?
  • How would you compute a running total that resets whenever the campaign is paused for more than three days?
  • Which is cheaper at scale: a window function here or a pre-aggregated cumulative table, and why?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data

Table: campaign_daily_spend(campaign_id, ds, spend_usd). I would confirm there is at most one row per campaign per day. If not, I would aggregate first, because duplicates change how the default fra...

Approach

A running total is SUM(spend) OVER (PARTITION BY ... ORDER BY ds). Adding DATE_TRUNC('month', ds) to the partition resets the sum on the first of each month. I spell out the frame as `ROWS BETWEEN...


Submit Your Answer
Markdown supported