N064-M1 Tier 5 · Expert · medium ecommerce · Brightlane

Return each (`order_month`, `status`) combination, the count of `orders` with that `status` in that month, and the running total of `orders` in that `status` from the earliest such month through the current month — restarting at the earliest month for each `status` independently

Part of Running Totals and Cumulative Metrics in SQL

The problem

Scenario: Brightlane's operations team is tracking cumulative order volume separately for each status, so each status has its own independent running total over time.

Task: Write a query to return each (order_month, status) combination, the count of orders with that status in that month, and the running total of orders in that status from the earliest such month through the current month — restarting at the earliest month for each status independently.

Assumptions:

  • An order month is identified by its first day.
  • A combination's monthly_count is the count of orders with that status placed in that month.
  • A combination's cumulative_count is the combined monthly_count for the same status from the earliest month containing that status through the current month — drawn only from entries of the same status, never across different statuses.

Output:

  • One row per (order_month, status) combination present in the data.
  • Columns in this order: order_month, status, monthly_count, cumulative_count.
  • Sorted by status ascending, then order_month ascending.
Schema · ecommerce 5 tables
categories
id integer
name text
parent_id? integer
products
id integer
name text
category_id integer
price numeric
stock_qty integer
attributes? jsonb
order_items
id integer
order_id integer
product_id integer
quantity integer
unit_price numeric
customers
id integer
name text
email text
city? text
country text
created_at timestamptz
is_active boolean
orders
id integer
customer_id integer
ordered_at timestamptz
status text
total_amount numeric

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution Try it yourself first
Solution query
SELECT
  DATE_TRUNC('month', ordered_at) AS order_month,
  status,
  COUNT(*) AS monthly_count,
  SUM(COUNT(*)) OVER (
    PARTITION BY
      status
    ORDER BY
      DATE_TRUNC('month', ordered_at) ROWS BETWEEN UNBOUNDED PRECEDING
      AND CURRENT ROW
  ) AS cumulative_count
FROM
  orders
GROUP BY
  DATE_TRUNC('month', ordered_at),
  status
ORDER BY
  status,
  order_month

The shape

Two things compose. The GROUP BY aggregates orders to one row per (month, status) pair with that pair's monthly count, and PARTITION BY status inside the window function keeps each status's running total isolated. Without PARTITION BY, the accumulation would cross status boundaries; with it, every status restarts at its own earliest month.

Clause by clause

  • SELECT DATE_TRUNC('month', ordered_at) AS order_month, status, COUNT(*) AS monthly_count produces one row per (month, status) combination with that pair's order count. Orders in March 2022 with status = 'cancelled' group together and produce monthly_count = 1 for that pair.
  • SUM(COUNT(*)) OVER (PARTITION BY status ORDER BY DATE_TRUNC('month', ordered_at) ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cumulative_count accumulates the monthly counts within each status independently. PARTITION BY status is the load-bearing piece: the running total for 'cancelled' ignores the rows for 'delivered' entirely, and the frame resets at each status's earliest month.
  • FROM orders GROUP BY DATE_TRUNC('month', ordered_at), status aggregates the raw orders to one row per (month, status) pair.
  • ORDER BY status, order_month sorts the final output by status first so each status's running total reads as a contiguous block.

Why PARTITION BY and not a separate query per status

Without PARTITION BY status, the window function would treat the full result as one stream and the running total would jump between statuses in whatever order the ORDER BY produced. A separate query per status would work but requires knowing every status in advance and stitching the results together. PARTITION BY lets the window function compute every status's independent running total in one pass.

The trap

The query's final ORDER BY status, order_month only changes the display order of the output. The window function's own ORDER BY DATE_TRUNC('month', ordered_at) is what determines which row counts as "previous" inside each partition. These are two independent ordering instructions. Changing the final ORDER BY to anything else (sorting by monthly_count, for instance) would change how the output prints but would not affect the cumulative values, because the window has already computed them against its own ordering.

You practiced partitioning the running total by status, so each status has its own independent cumulative line — without partitioning, the total would cross status boundaries and produce a single global running count.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.

Practice, feedback, mastery, review. That's the loop that turns reading into real skill.

Start free

No account, no credit card. Start solving in under a minute.