Basics
- How to Insert
- How to Update
- How to Delete
- How to Trim Strings
- How to Use substring()
- How to Use substring() with RegEx to Extract a String
- How to Replace Substrings
- How to Modify Arrays
- How to Compare Arrays
- How to Concatenate Strings
- How to Convert the Case of a String
- How to Create an Array
- How to Insert Data Into an Array
- How to Query Arrays
- How to Use string_agg()
Database Management
- How to Create a Table
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Change a Column Name
- How to Add a Default Value
- How to Remove a Default Value From a Column
- How to Add a Not Null Constraint
- How to Remove a Not Null Constraint
- How to Add an Index
- How to Drop an Index
- How to Create a View
- How to Drop a View
- How to Reset Sequence
- How to Drop a Column
Dates and Times
Analysis
- How to Use nullif()
- How to Use Lateral Joins
- How to Calculate Percentiles
- How to Get the First Row per Group
- How to Use generate_series to Avoid Gaps in Data
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Import a CSV using Copy
- How to Compare Two Values When One Is Null
- How to Use Coalesce
- How to Write a Case Statement
- How to Use Filter to Have Multiple Counts
- How to Calculate Cumulative Sum-Running Total
- How to Query a JSON Column
How to Calculate Cumulative Sum-Running Total in PostgreSQL
In PostgreSQL, calculating cumulative sums or running totals is a common operation that can be achieved using window functions. These functions allow you to perform calculations across a set of table rows that are related to the current row. In this tutorial, we’ll explore how to calculate a cumulative sum using the SUM()
function combined with the OVER()
clause.
What is a Running Total?
A running total, or cumulative sum, is a sequence of partial sums of a given data set. For example, in a list of daily sales, a running total would show the total sales up to each day. This can be particularly useful in financial reports or time-based data analysis.
Step 1: Prepare Your Data
Let’s start by creating a sample table with some sales data.
-- Create a sample sales table
CREATE TABLE sales (
id SERIAL PRIMARY KEY,
sales_date DATE,
amount DECIMAL
);
-- Insert sample data
INSERT INTO sales (sales_date, amount) VALUES
('2025-01-01', 100.00),
('2025-01-02', 200.00),
('2025-01-03', 150.00),
('2025-01-04', 50.00);
Step 2: Calculate Cumulative Sum
Now that we have our sales data, let’s calculate the cumulative sum using the SUM()
window function. The OVER()
clause will allow us to accumulate the values over a specific order.
-- Calculate the running total
SELECT
sales_date,
amount,
SUM(amount) OVER (ORDER BY sales_date) AS running_total
FROM sales
ORDER BY sales_date;
Explanation
- The
SUM(amount)
function calculates the sum of theamount
column. - The
OVER (ORDER BY sales_date)
clause defines the window for the calculation, ordering the rows bysales_date
so that the sum is calculated sequentially. - The
running_total
column shows the cumulative sum at each point in time.
Result
The query will return a result similar to the following:
sales_date | amount | running_total
------------+--------+--------------
2025-01-01 | 100.00 | 100.00
2025-01-02 | 200.00 | 300.00
2025-01-03 | 150.00 | 450.00
2025-01-04 | 50.00 | 500.00
Step 3: Adding Partitioning (Optional)
In some cases, you may want to calculate the running total within a specific category, such as a department or a region. You can do this by using the PARTITION BY
clause. For example, let’s say we have multiple regions, and we want to calculate the running total per region.
-- Add partitioning to calculate running total by region
SELECT
region,
sales_date,
amount,
SUM(amount) OVER (PARTITION BY region ORDER BY sales_date) AS running_total
FROM sales
ORDER BY region, sales_date;
Conclusion
In this tutorial, we’ve learned how to calculate the cumulative sum or running total in PostgreSQL using the SUM()
function with the OVER()
clause. By using window functions, you can easily calculate running totals over any ordered or partitioned data set, making it an essential tool for data analysis.
For more advanced SQL techniques and tutorials, stay tuned to our blog!