Calculate Running Total in SQL

Introduction

Running totals are a vital concept in SQL, allowing you to calculate cumulative sums of values in your data. However, there are two distinct types of running totals to consider: Unpartitioned and Partitioned. In this article, we will explore these two types, their applications, and provide examples to help you grasp their significance in data analysis.

Unpartitioned Running Totals

Unpartitioned running totals are calculated over the entire dataset without any resets or specific criteria.

They are handy when you need to track cumulative values across all data, such as when calculating cumulative sales over time.

Example

Suppose we have a table called Orders with the following data.

--Create Table
CREATE TABLE Orders (
  Order_Date DATE,
  Customer_Id INT,
  Sales INT
);

-- Insert some data into the table
INSERT INTO Orders (Order_Date, Customer_Id, Sales) VALUES
('2023-01-01', 1, 100),
('2023-01-02', 1, 50),
('2023-01-03', 2, 200),
('2023-01-04', 2, 100),
('2023-01-05', 1, 100),
('2023-01-06', 2, 100);

Table Output

To calculate an Unpartitioned running total of sales, you can use the following SQL query.

SELECT Customer_Id,Order_Date,Sales,
SUM(Sales) OVER (ORDER BY Order_Date) AS Running_Total
FROM Orders Order By Order_Date;

Output

The result accumulates the running total across all dates without any resets.

unpartitioned running total example

Partitioned Running Totals

Partitioned running totals are calculated over a subset of the data based on specific criteria or partitions.

They are useful when you want to calculate totals within specific categories or groups, like counting daily sales separately or calculating totals for different customer segments.

Example

Suppose we have the same table called Orders as above in the unpartitioned example.

To calculate a Partitioned running total of sales within each customer_id, you can use the following SQL query.

SELECT Customer_Id,Order_Date,Sales,
SUM(Sales) OVER (PARTITION BY Customer_Id ORDER BY Order_Date) AS Running_Total
FROM Orders Order By Customer_Id,Order_Date

The result accumulates the running total separately for each customer_id.

partitioned running total example

Summary

In this article, we learn about how to calculate running totals in SQL. Running totals are powerful tools for cumulative calculations. Understanding the difference between unpartitioned and partitioned running totals is essential for effective data analysis.

Unpartitioned running totals accumulate values across the entire dataset, while Partitioned running totals allow you to calculate totals within specific partitions, making them invaluable for segmenting and analyzing data. Incorporate these concepts into your SQL projects to enhance your data analysis capabilities.

If you find this article valuable, please consider liking it and sharing your thoughts in the comments.

Thank you, and happy coding!


Similar Articles