Calculate Running Total in SQL
Last Updated :
27 Apr, 2021
Structured Query Language or SQL is a standard Database language that is used to create, maintain and retrieve the data from relational databases like MySQL, Oracle, etc. In this article, we will be using the Microsoft SQL Server.
Here we are going to see how to get the running salary total of each department. Here, we will first create a database named “geeks” then we will create a table “department” in that database. After, that we will execute our query on that table.
Creating Database:
CREATE geeks;
To use this database:
USE geeks;
This is our table in the geek's database:
CREATE TABLE department(
ID int,
SALARY int,
NAME Varchar(20),
DEPT_ID Varchar(255));
Output:
Command(s) completed successfully.
Add value into the table:
INSERT INTO department
VALUES (1, 34000, 'ANURAG', 'UI DEVELOPERS');
INSERT INTO department
VALUES (2, 33000, 'harsh', 'BACKEND DEVELOPERS');
INSERT INTO department
VALUES (3, 36000, 'SUMIT', 'BACKEND DEVELOPERS');
INSERT INTO department
VALUES (4, 36000, 'RUHI', 'UI DEVELOPERS');
INSERT INTO department
VALUES (5, 37000, 'KAE', 'UI DEVELOPERS');
Output:
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
Select in SQL Server Management Studio:
This is our data inside the table:
SELECT * FROM department;
ID | SALARY | NAME | DEPT_ID |
---|
1 | 34000 | ANURAG | UI DEVELOPER |
2 | 33000 | HARSH | BACKEND DEVELOPERS |
3 | 36000 | SUMIT | BACKEND DEVELOPERS |
4 | 36000 | RUHI | UI DEVELOPER |
5 | 37000 | KAE | UI DEVELOPER |
Select in SQL Server Management Studio:
Example 1:
Query to Calculate Running Total in SQL Server
SELECT * ,(
SELECT SUM(T2.[SALARY])
FROM [department] AS T2
WHERE T2.[ID] <= T1.[ID]
) AS [Running Total]
FROM [department] AS T1
Output:
ID | SALARY | NAME | DEPT_ID | Running Total |
---|
1 | 34000 | ANURG | UI DEVELOPER | 34000 |
2 | 33000 | HARSH | BACKEND DEVELOPERS | 67000 |
3 | 36000 | SUMIT | BACKEND DEVELOPERS | 103000 |
4 | 36000 | RUHI | UI DEVELOPER | 139000 |
5 | 37000 | KAE | UI DEVELOPER | 176000 |
Select in SQL Server Management Studio:
Example 2
In this SQL Server example, we'll use the SUM Function and OVER to find the Running Total.
Query to Calculate Running Total in SQL Server
SELECT *
,SUM([SALARY]) OVER (
ORDER BY [ID]
) AS [Running Total]
FROM department
Output:
ID | SALARY | NAME | DEPT_ID | Running Total |
---|
1 | 34000 | ANURAG | UI DEVELOPER | 34000 |
2 | 33000 | HARSH | BACKEND DEVELOPERS | 67000 |
3 | 36000 | SUMIT | BACKEND DEVELOPERS | 103000 |
4 | 36000 | RUHI | UI DEVELOPER | 139000 |
5 | 37000 | KAE | UI DEVELOPER | 176000 |
Select in SQL Server Management Studio:
Example 3:
In this SQL Server example, we will use PARTITION BY with OVER to find the Running Total.
Query to Calculate Running Total in SQL Server
SELECT *
,SUM([SALARY]) OVER (
PARTITION BY DEPT_ID ORDER BY Id
) AS [Running Total]
FROM department
Output:
ID | SALARY | NAME | DEPT_ID | Running_Total |
---|
2 | 33000 | HARSH | BACKEND DEVELOPERS | 33000 |
3 | 36000 | SUMIT | BACKEND DEVELOPERS | 69000 |
1 | 34000 | ANURAG | UI DEVELOPER | 34000 |
4 | 36000 | RUHI | UI DEVELOPER | 70000 |
5 | 37000 | KAE | UI DEVELOPER | 107000 |
Select in SQL Server Management Studio:
Similar Reads
How to Calculate Retention Rate in SQL?
The retention rate is calculated by counting the number of users who return on a regular basis, such as every week or month, and grouping them by the week they signed up. Suppose the user, who is a student of X School needs to login to the portal every day in order to access the study materials teac
4 min read
Calculate Moving Averages in SQL
In data analysis, smoothing out short-term fluctuations in time-series data is essential for identifying long-term trends. One effective method for achieving this is through the moving average, a widely used technique in business analytics, finance and forecasting. SQL provides several ways to compu
4 min read
MySQL | Creating stored function
The CREATE FUNCTION statement is used for creating a stored function and user-defined functions. A stored function is a set of SQL statements that perform some operation and return a single value. Just like Mysql in-built function, it can be called from within a Mysql statement. By default, the stor
2 min read
SQL Query to Exclude Null Values
In relational databases, managing NULL values is a critical aspect of data integrity and accuracy. A NULL value signifies the absence of data in a column, distinguishing it from a zero, which is an integer, or a blank space, which is a character. Queries involving NULL values require careful handlin
3 min read
Python MySQL - GROUP BY and HAVING Clause
In this article, we will see how to perform groupby() and HAVING() operations on SQL using Python. Here we will consider a college database to perform group by operation on the department with respect to student strength. GROUP BY The GROUP BY statement groups rows that have the same values into sin
2 min read
Val() and Sum() Function in MS Access
1. Val() Function : val() function returns the number found in the string. In this function, it takes a string as a parameter and it will return the number in the string. Note : This function will stop reading when the first non-numeric character comes. Syntax : Val(string) Example-1 : SELECT Val("2
1 min read
STD() function in MySQL
With the help of STD() function we can calculate population Standard deviation of an expression in MySQL. But, if there are no matching rows in the given expression it returns Null. Syntax : STD(expr); Parameter : This method accepts only one parameter. expr : Input expression from which we want to
3 min read
ADDDATE() function in MySQL
The ADDDATE() function in MySQL is a powerful tool for adding specific time intervals to date or datetime values. It simplifies data manipulation by allowing us to easily calculate future or adjusted dates based on a given starting point. In this article, we will learn about the ADDDATE() function i
3 min read
SQL COUNT() with GROUP BY Clause
The SQL COUNT() function is a powerful tool used to count the number of rows in a dataset. When combined with the GROUP BY clause, it helps group data by specific attributes and count rows within each group. This is particularly useful for summarising data and generating insights.In this article, we
3 min read
SQL Count() Function
In the world of SQL, data analysis often requires us to get counts of rows or unique values. The COUNT() function is a powerful tool that helps us perform this task. Whether we are counting all rows in a table, counting rows based on a specific condition, or even counting unique values, the COUNT()
7 min read