Brightlane's finance report shows each order's amount alongside both the overall total across every order and the running total accumulated up to that order.
Write a query to return the ID and amount of every order, plus the grand total of total_amount across every order and the running total accumulated through that order in order of id.
Assumptions:
- The
orderstable has one row per order with anidand atotal_amount. - The grand total is the combined
total_amountacross every order in the table. The same value should appear on every output row. - The running total at each row is the combined
total_amountof every order whoseidis less than or equal to that row'sid. On the row with the largestid, the running total equals the grand total.
Output:
- One row per order, with columns
id,total_amount,grand_total, andrunning_total.
Schema · ecommerce 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
id,
total_amount,
SUM(total_amount) OVER () AS grand_total,
SUM(total_amount) OVER (
ORDER BY
id
) AS running_total
FROM
orders The shape
Two windowed SUM expressions sit side by side and disagree on ORDER BY. SUM(total_amount) OVER () has no ORDER BY inside OVER, so it returns the grand total on every row. SUM(total_amount) OVER (ORDER BY id) has an ORDER BY, so it returns a running total. The presence or absence of ORDER BY inside OVER is the entire difference between the two columns.
Clause by clause
SELECT id, total_amountreturns each order's identifier and amount. Both windowed sums are computed alongside.SUM(total_amount) OVER () AS grand_totalis the static window. The emptyOVER ()defines the window as every row in the table, with no ordering applied. TheSUMcollapses across that whole window and returns the same value,126725.73, on every row. It is the same number a plainSELECT SUM(total_amount) FROM orderswould return, just attached to every row instead of collapsing the table.SUM(total_amount) OVER (ORDER BY id) AS running_totalis the accumulating window.ORDER BY idinsideOVERdefines the window for each row as every row with anidless than or equal to the current one. The result grows row by row. On the lastid,running_totalequalsgrand_total, which is the visual confirmation that the accumulation has reached the full table.FROM ordersreads every order. Both windowed sums apply to the same row set; their results differ only because of whatORDER BYdoes insideOVER.
Why two windowed SUM expressions and not one grouped sum
A GROUP BY would collapse the rows. The report needs the per-order detail and the totals attached. Two windowed sums with different OVER clauses on the same row set is the canonical shape for that ask. The window mechanism is what makes both totals available on every order row at the same time.
You practiced two windows in one query — SUM OVER () for the static grand total and SUM OVER (ORDER BY ...) for the running total — same aggregate, two window definitions, two distinct meanings.