SQL
Calculate a Running Total (Cumulative Sum)
Learn how to calculate a cumulative sum or running total for a column in SQL, useful for trend analysis and financial reporting over a sequence of data.
SELECT
order_date,
amount,
SUM(amount) OVER (ORDER BY order_date ASC) as running_total
FROM
orders
ORDER BY
order_date ASC;
How it works: This SQL query calculates a running total (cumulative sum) for the amount column based on the order_date. The SUM(amount) OVER (ORDER BY order_date ASC) window function computes the sum of amount values up to the current row, ordered by order_date. This is highly useful for tracking cumulative metrics like sales over time.