SUM is another commonly used function in SQL after the COUNT. SUM is used to get the sum/addition of the records in the table.
It is very simple to use the SUM function in SQL / Hive query. But, How to calculate Cumulative Sum / Running Total?
For example, the cumulative sums of the sequence {a,b,c,...}, are a, a+b, a+b+c, ....
It is very simple to use the SUM function in SQL / Hive query. But, How to calculate Cumulative Sum / Running Total?
[Def] CUMULATIVE SUM :
A cumulative sum is a sequence of partial sums of a given sequence.For example, the cumulative sums of the sequence {a,b,c,...}, are a, a+b, a+b+c, ....
Recommended SQL Courses:
Here, we will be demonstrating the calculation of cumulative sum using SQL query using the following problem statement.
Let's consider a tables "EMPLOYEE" as shown below:
Table created: EMPLOYEE
We want to get the output as:
INNER JOIN keyword returns the records that have matching values in both the tables. In this case, we are joining the same table "EMPLOYEE" with itself.
The condition for joining these tables will be (A.ID >= B.ID)
Inner join on (A.ID >= B.ID) condition is shown in below image.
Let's consider a tables "EMPLOYEE" as shown below:
Table created: EMPLOYEE
EXPECTED OUTPUT :
We need to perform a cumulative sum on the column SALARY of the table employee.We want to get the output as:
■ INNER JOIN LOGIC :
In order to get the result, as shown above, we need to perform an inner join on the same table.INNER JOIN keyword returns the records that have matching values in both the tables. In this case, we are joining the same table "EMPLOYEE" with itself.
The condition for joining these tables will be (A.ID >= B.ID)
Inner join on (A.ID >= B.ID) condition is shown in below image.
■ VIDEO :
■ QUERY :
OUTPUT :
That's it. We got the expected output.
--------------------------------------------------------------------------------
Click here to see solutions for all Machine Learning Coursera Assignments.
&
Click here to see more codes for Raspberry Pi 3 and similar Family.
&
Click here to see more codes for NodeMCU ESP8266 and similar Family.
&
Click here to see more codes for Arduino Mega (ATMega 2560) and similar Family.
Feel free to ask doubts in the comment section. I will try my best to answer it.
If you find this helpful by any mean like, comment and share the post.
This is the simplest way to encourage me to keep doing such work.
Thanks & Regards,
-Akshay P Daga