Date Operations & Time-Based Analytics SQL Topic exerciseMediumVerified answerSQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

Calculate May 2024 Month-to-Date Revenue

Calculate daily revenue and month-to-date revenue for May 2024.

  • Window functions
  • Aggregation
  • Date analysis
  • Filtering
  • Sorting

Exercise brief

Understand the request

Revenue operations analyst A month-close review needs daily May revenue and the cumulative total through each observed day.

A month-close review needs daily May revenue and the cumulative total through each observed day. Calculate daily revenue and month-to-date revenue for May 2024.

Return

  • Return order_date, daily_revenue, mtd_revenue in this exact left-to-right order.

Constraints

  • Filter to May 2024 with a deterministic period.
  • Aggregate to day grain before applying the running total.
  • Use an explicit ROWS frame and order by order_date.

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

orders

  • order_idINTEGER
  • order_dateDATE
  • order_totalDECIMAL

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

Filter the source rows to the requested year-month first.

Hint 2

SUM per order_date produces daily revenue.

Hint 3

Apply a windowed SUM to those daily totals with an explicit cumulative ROWS frame.

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
SELECT order_date, SUM(order_total) AS daily_revenue, SUM(SUM(order_total)) OVER (ORDER BY order_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS mtd_revenue FROM orders WHERE strftime('%Y-%m', order_date) = '2024-05' GROUP BY order_date ORDER BY order_date;

Why this works

MTD is a running total within a fixed month. Aggregating first prevents multiple orders on one date from creating duplicate day rows.

Success check

Both observed May dates appear in order and the second MTD value equals total May revenue.

Expected result

Use this output to verify values, aliases, ordering, and row count.

order_datedaily_revenuemtd_revenue
2024-05-05220220
2024-05-20380600

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.