I have always been confused between cumulative, running, and rolling sums. This is just a short note to avoid that confusion.

Simple Aggregate Sum

The standard, basic use of the SUM() function without a WINDOW clause.

-- Calculating the total sales for an entire year or for a specific region.
SELECT SUM(sale_amount) AS total_sales
FROM sales
WHERE sale_date BETWEEN '2023-01-01' AND '2023-12-31';

Cumulative Sum/Running Total

It calculates the sum of the current row and all preceding rows within the result set, based on a defined order.

sale_datesale_amountcumulative_sum
2025-01-0110001000
2025-01-025001500
2025-01-0315003000
2025-01-0420005000
2025-01-0530008000
2025-01-063008300
-- Tracking how much total revenue has accumulated day-by-day 
-- until today
SELECT
    sale_date,
    sale_amount,
    SUM(sale_amount) OVER (ORDER BY sale_date) AS cumulative_sum
FROM
    daily_sales

Partitioned Cumulative Sum

The cumulative sum that resets when a specified grouping column changes. It uses the PARTITION BY clause within the window definition.

-- Calculating a running total for sales _within each product category separately.
SELECT
    category,
    sale_date,
    sale_amount,
    SUM(sale_amount) OVER (
        PARTITION BY category
        ORDER BY sale_date
    ) AS category_running_total
FROM
    sales_by_category;
 

Rolling Sum (Moving Window Sum)

This type of sum calculates the aggregate over a “moving” window of a fixed size, either by a number of ROWS or a time RANGE

-- Calculating a 7-day average of sales, where the window slides forward with each new day.
SELECT
    sale_date,
    sale_amount,
    SUM(sale_amount) OVER (
        ORDER BY sale_date
        ROWS BETWEEN 6 PRECEDING AND CURRENT ROW -- Current row + 6 preceding rows = 7 total rows
    ) AS seven_day_rolling_sum
FROM
    daily_sales;
-- 7 calendar days back
-- We use `INTERVAL '6 DAY' PRECEDING` to sum the current day and the 6 days prior, totaling a 7-day period.
SELECT
    sale_date,
    sale_amount,
    SUM(sale_amount) OVER (
        ORDER BY sale_date
        RANGE BETWEEN INTERVAL '6 DAY' PRECEDING AND CURRENT ROW
    ) AS seven_day_rolling_sum
FROM
    daily_sales;
 
sale_datesale_amountweekly_rolling_sum
2024-01-01100.00100.00
2024-01-02150.00250.00
2024-01-0475.00325.00 (Sums Jan 1, 2, 4. Jan 3 is missing in data but covered by the date range)
2024-01-05200.00425.00 (Sums Jan 1, 2, 4, 5. Jan 1 is within 6 days of Jan 5)
2024-01-0650.00475.00 (Sums Jan 1, 2, 4, 5, 6)
2024-01-08125.00450.00 (Sums Jan 2, 4, 5, 6, 8. Jan 1 is outside the 6-day window)