>_QueryLocalCSV & JSON tools that run in your browser

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 on the demo dataset →

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).

Want to run it on your own file? Open it with Query CSV with SQL: your file becomes the table data, and you can paste this query with your own column names.

Related recipes