Lesson 7 of 14 · 26 min
Running totals and the default frame trap
By the end of this lesson you can build running (cumulative) totals and counts, restart them per group, explain the default window frame, and use ROWS to get a true row-by-row total when the ORDER BY has duplicates.
In the PARTITION BY lesson you saw that adding ORDER BY inside OVER turns SUM into something that grows row by row. That is the running total: each row shows the sum of itself and every row before it in the window's order.
The pattern for year-to-date revenue is: aggregate to one row per month, then put a running SUM over the months.
This lesson, its 4 graded exercises and every other Members lesson in SQL Quest come with a Formula Quest membership that includes SQL (Single Quest with SQL selected, Quest Trio including SQL, or Quest Unlimited), or with SQL access from your organization.
Free to everyone: all of SQL Foundations and Retrieving and Filtering Data, the first lesson of every track, the playground, the dataset library and the SQL reference. See free lessons