>_ QueryLocal All recipes
SQL recipe

How do I compute a running total over time with SQL?

SELECT
  order_date,
  quantity * unit_price AS line_total,
  SUM(quantity * unit_price) OVER (ORDER BY order_date) AS running_total
FROM data
ORDER BY order_date;
Run this query in QueryLocal →

A window function without PARTITION BY runs over the whole result set: `SUM(...) OVER (ORDER BY order_date)` adds each row's value to the running sum of every prior row in date order, producing a cumulative total column alongside the original rows — the SQL equivalent of a spreadsheet's running-total formula.

A table named data with columns: order_id (integer), customer (text), country (text), category (text), quantity (integer), unit_price (real), order_date (text, YYYY-MM-DD).