SQL

Calculate Running Total (Cumulative Sum) in SQL

Learn how to compute a running total or cumulative sum over a set of rows in SQL using window functions, essential for financial reports and trend analysis.

SELECT
    order_date,
    amount,
    SUM(amount) OVER (ORDER BY order_date ROWS UNBOUNDED PRECEDING) AS running_total
FROM
    Sales;
How it works: This query calculates a running total of `amount` based on `order_date`. The `SUM() OVER (ORDER BY order_date ROWS UNBOUNDED PRECEDING)` window function ensures that for each row, the sum includes all `amount` values from the beginning of the dataset up to the current row's `order_date`, ordered chronologically within the entire dataset.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs