## Running Total: Understanding and Computing in SQL

In today’s digital era where data is abundant, one of the most critical tasks is to analyze and manage it effectively. Many applications, like online shops, social networking websites, and financial systems, generate a massive amount of data every day.

To make informed decisions, data needs to be processed effectively. In this regard, the SQL language is one of the most popular tools for data manipulation.

One essential operation in SQL is calculating the running total. This article will detail the concept of SQL running total, its uses, and how to compute it.

## Definition of Running Total

A running total, also known as a cumulative sum, is a calculation that continuously sums the previously calculated values. Formally, a running total is the sum of a column of numbers over time.

A running total calculates values at each row, including previous rows’ sum. In SQL, a running total is calculated using window functions.

### Example of Running Total in Online Shop Registration

An online shop requires user registration to keep track of customer details and their buying history. Suppose the shopkeeper wants to have a running total of the registered users continually.

### They can use the following SQL query:

```
Select COUNT(*) OVER (ORDER BY registration_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as running_total_users
FROM user_registration;
```

This query will select the running total of registered users in the User_Registration table calculated based on the registration_date column.

### Example of Running Total in Company Revenue

Another example of a running total is in calculating company revenue for a financial year. The calculation is simplified by using the SQL query:

### SELECT SUM(revenue) OVER (ORDER BY month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as running_total_revenue

`FROM company_financials;`

In this code, the query selects the running total for company revenue in the Company_Financials table. It works by summing up the revenue column for each row and calculating the cumulative sum of the values.

## Availability of Window Functions for Running Total Calculation

SQL Window Functions are a feature for calculating running totals. They offer a simplified way of computing aggregate values over a set of rows.

These functions also provide flexibility, allowing the use of different window specifications, such as the range of rows to include, the grouping of rows, and the ordering of rows.

## How to Compute a Cumulative Sum in SQL

To compute a cumulative sum, SQL provides window functions that simplify the calculation. There are various types of windows, such as sliding windows, hopping windows, and tumbling windows, which can be used for different data processing applications.

However, we shall focus on how to use window functions to compute a cumulative sum.

## Usage of Window Functions for Cumulative Sum Computation

Windows functions provide an excellent way of calculating the cumulative sum. The OVER() function defines a window with start and endpoint and allows the window function to compute over the specified range.

For example, to calculate the cumulative sum of the number of registered shop users, an SQL query is used:

```
SELECT user_id, SUM(number_of_users) OVER (ORDER BY registration_date)
FROM user_registration;
```

This function will display the User ID number, alongside the cumulative sum of the number_of_users column calculated based on the registration_date column.

## Syntax of SQL Window Function to Compute Cumulative Sum

### The syntax of a cumulative sum calculation using a window function is as follows:

```
SELECT Column, SUM(Column) OVER (ORDER BY OrderColumn ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as CumulativeSum
FROM DataTable;
```

The above syntax selects the column that contains the data we want to compute the cumulative sum. It then subtracts the OrderColumn as the column of rows within the window range.

The UNBOUNDED PRECEDING expression defines an infinite range starting from the beginning of the table to the current row.

### Example of Cumulative Sum for Running Total Calculation on Registered Users

Consider a scenario where a company wants to calculate the running total on the number of registered employees from the beginning up to a particular time. We can use the following query:

```
SELECT employee_id, SUM(number_of_employees) OVER (ORDER BY employment_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as CumulativeSum
FROM employee_data;
```

In the above script, the Employee_ID, alongside the CumulativeSum of the number_of_employees column, is selected from the Employee_Data table.

The CumulativeSum column is calculated over the rows between an unbounded preceding and the current row considering the Employment_Date column.

## Conclusion

SQL running total concepts presented in this article provide a firm foundation for data manipulation. They help us to make informed business decisions and provide a better understanding of the data being processed.

SQL window functions provide a straightforward and flexible way to calculate a cumulative sum in SQL, and it is essential to use them in data analysis. With these essential skills, you will have the much-needed knowledge to be a professional SQL data analyst.

## More Examples

In the previous section, we discussed the concept of SQL running total, its definition, and how to compute it. This section will include two more examples of how to calculate a running total using SQL.

The first example will show how to calculate a running total for registered users. The second example will illustrate how to partition the running total by country.

### Example 1: Calculating Running Total on Registered Users

In this example, suppose we have a table called “registered_users,” which stores the number of registered users and their registration dates. The table has two columns: “registration_date” and “number_of_users.”

### Usage of SQL Query to Calculate Running Total on Registered Users

To calculate the running total for registered users, we can use the following SQL query:

```
SELECT registration_date, SUM(number_of_users) OVER(ORDER BY registration_date) AS running_total_users
FROM registered_users;
```

This query will select the registration date column and the cumulative sum of the number_of_users column calculated based on the registration_date column.

### Example Table for Handling Registered Users and their Registration Dates

Suppose the table “registered_users” contains the following data:

Registration_Date | Number_of_Users |
---|---|

2021-01-01 | 10 |

2021-01-02 | 15 |

2021-01-03 | 20 |

2021-01-04 | 5 |

2021-01-05 | 12 |

### Explanation of Running Total on Registered Users based on Example Table

This output shows the registration dates and the cumulative sum of the registered users’ total. The SQL query first sorts the rows by registration date and then calculates the cumulative sum up to and including the current row.

Therefore, the output shows how many registered users that happen until a particular point in time.

### The result will look like the following table:

Registration_Date | Running_Total_Users |
---|---|

2021-01-01 | 10 |

2021-01-02 | 25 |

2021-01-03 | 45 |

2021-01-04 | 50 |

2021-01-05 | 62 |

### Example 2: Partitioning Running Total on Registered Users based on Country

When analyzing data, we sometimes need to calculate a running total based on subsets of data.

In this example, we will partition the running total by country. Suppose we have a table called “registered_users_by_country,” which stores the number of registered users, their registration dates, and the countries they registered from.

### Usage of SQL Query to Partition Running Total by Country

To partition the running total by country, we can use the following SQL query:

```
SELECT country, registration_date, SUM(number_of_users) OVER(PARTITION BY country ORDER BY registration_date) AS running_total_users
FROM registered_users_by_country;
```

This query will select the country column, registration_date, and the cumulative sum of the number_of_users column partitioned by Country and calculated based on the registration_date column.

### Example Table for Registered Users and their Registration Dates by Country

Suppose the table “registered_users_by_country” contains the following data:

Country | Registration_Date | Number_of_Users |
---|---|---|

United States | 2021-01-01 | 10 |

United States | 2021-01-02 | 15 |

United States | 2021-01-03 | 20 |

Canada | 2021-01-01 | 5 |

Canada | 2021-01-02 | 10 |

Canada | 2021-01-03 | 15 |

### Explanation of Running Total Partitioning by Country based on Example Table

This output shows the running total of the registered users partitioned by their countries in ascending order based on their registration dates. By using the PARTITION BY and ORDER BY clauses, we can indicate how the table should be grouped and ordered.

The result indicates the number of registered users at a particular date for each country. The result will look like the following table:

Country | Registration_Date | Running_Total_Users |
---|---|---|

Canada | 2021-01-01 | 5 |

Canada | 2021-01-02 | 15 |

Canada | 2021-01-03 | 30 |

United States | 2021-01-01 | 10 |

United States | 2021-01-02 | 25 |

United States | 2021-01-03 | 45 |

## Conclusion

SQL running total and partitioning are essential analytical techniques in data science. They provide a simple and flexible way to evaluate your data by calculating aggregated values over a range of rows.

This article has illustrated how SQL queries can be used to calculate a running total and added two more examples to demonstrate how the process could be used. We hope that you can use these SQL techniques in your data science analysis to obtain insights that can help your business make informed decisions.

## Additional Example: Cumulative Score

In the previous section, we discussed how to calculate a running total and partition it based on a specific column. This section will introduce another example of how to compute a cumulative score for gamers participating in two different games.

### Example 3: Computing Cumulative Score for Gamers in Two Different Games

Consider a scenario where company XYZ hosts two games, game A and game B, and the gamers score points as they pass through different levels of the game.

Suppose we have a table that stores the game ID, gamer ID, game level, competition date, and score.

### Usage of SQL Query to Calculate Cumulative Score for Gamers in Two Games

To calculate the cumulative score for gamers playing two different games, we can use a SQL query to generate the results. We will use partition by country syntax in this example.

### The query is as follows:

```
SELECT GameID, GamerID, GameLevel, CompetitionDate, Score,
SUM(Score) OVER(PARTITION BY GameID, GamerID ORDER BY CompetitionDate) AS CumulativeScore
FROM Game_Scores;
```

This query will select the GameID, GamerID, GameLevel, CompetitionDate, Score columns and the cumulative sum of the scores partitioned by the game ID and GamerID columns and ordered by CompetitionDate.

### Example Table for Storing Game ID, Gamer ID, Game Level, Competition Date, and Score

Suppose the table “Game_Scores” contains the following data:

Game ID | Gamer ID | Game Level | Competition Date | Score |
---|---|---|---|---|

A | 1001 | Level 1 | 2022-01-01 | 10 |

A | 1001 | Level 2 | 2022-01-09 | 30 |

B | 2002 | Level 1 | 2022-01-02 | 20 |

B | 2002 | Level 2 | 2022-01-15 | 40 |

### Explanation of Cumulative Score Calculation for Gamers in Two Games based on Example Table

This output shows the GameID, GamerID, GameLevel, CompetitionDate, and scores received. The column “CumulativeScore” calculates the running total of scores partitioned by GamerID and Game ID and ordered by competition date.

### The results will look like the following table:

Game ID | Gamer ID | Game Level | Competition Date | Score | Cumulative Score |
---|---|---|---|---|---|

A | 1001 | Level 1 | 2022-01-01 | 10 | 10 |

A | 1001 | Level 2 | 2022-01-09 | 30 | 40 |

B | 2002 | Level 1 | 2022-01-02 | 20 | 20 |

B | 2002 | Level 2 | 2022-01-15 | 40 | 60 |

The above table provides insight into gamers’ progress, such as when they start and when they score. With the cumulative score now known, it is possible to see who is trailing and who is well ahead of the pack in each game.

## Benefits of Using Running Total in SQL Reports

The significance of running total in SQL reports is especially crucial for financial specialists, where trend analysis is required. Here are some ways running total can be harnessed to improve financial analysis:

### Impactful Instances of Running Total use in Financial and Trend Analysis

- Tracking sales performance – Running total helps track sales performance over time by showing the accumulation of sales by day, week, or month.
- Forecasting revenues – Running total can be used to create accurate revenue forecasts by providing insight into past performance.
- Inventory control – Running total can track inventory as it flows in and out of a business, helping to optimize stock levels and reduce waste.

## Suggested Further Study with Window Functions for Running Total Calculation

Window functions provide a convenient and efficient way to calculate running totals within SQL. These functions are ideal for large data sets, where traditional aggregate functions would be inefficient.

Further study into these functions for computation of running total, partitioning calculations, and window specifications is recommended.

## Conclusion

This article has explored examples of running total calculation for registered users, partitioning by country, and gamers in two different games. The use of running total in SQL reports is beneficial, especially with trend analysis, inventory control, and forecasting revenue being crucial aspects of a business.

Meanwhile, further study into window functions is highly recommended for more comprehensive data analysis. Employing the techniques outlined in this article can help optimize business operations and make informed decisions.

In summary, the article has delved into the concept of running total in SQL calculation, covering three examples, namely calculating running totals for registered users, partitioning running total by country, and calculating cumulative scores for gamers in two different games. We’ve examined how these calculations are achieved using SQL queries and discussed how they can be beneficial to business decisions and financial analysis.

Furthermore, the article has emphasized the importance of window functions in performing running total calculations and provided a recommendation for further study. The extensive utilization of running total in various operations can help optimize business operations and provide crucial insights into past performances and aid in making data-driven decisions.