SQL
Calculate Running Total (Cumulative Sum)
Learn to calculate cumulative sums or running totals for financial transactions or sequential data using SQL window functions, providing invaluable insights into progressive data trends.
SELECT
order_date,
amount,
SUM(amount) OVER (ORDER BY order_date) AS running_total
FROM
sales_orders
ORDER BY
order_date;
How it works: This query uses a window function `SUM() OVER (ORDER BY order_date)` to calculate a running total. For each row, it sums the `amount` of the current row and all preceding rows based on the `order_date` sequence, providing a cumulative sum for analytical purposes. This is particularly useful for tracking financial growth or sequential progress over time.