
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to create a Cumulative Sum Column in MySQL?
To create a cumulative sum column in MySQL, you need to create a variable and set to value to 0. Cumulative sum increments the next value step by step with current value.
Firstly, you need to create a variable with the help of SET. The syntax is as follows −
set @anyVariableName:= 0;
The syntax to create a cumulative sum column in MySQL is as follows −
select yourColumnName1,yourColumnName2,........N,(@anyVariableName := @anyVariableName + yourColumnName2) as anyVariableName from yourTableName order by yourColumnName1;
To understand the above concept, let us create a table. The following is the query to create a table −
mysql> create table CumulativeSumDemo −> ( −> BookId int, −> BookPrice int −> ); Query OK, 0 rows affected (0.67 sec)
Insert some records in the table with the help of select statement. The query to insert record is as follows −
mysql> insert into CumulativeSumDemo values(101,400); Query OK, 1 row affected (0.15 sec) mysql> insert into CumulativeSumDemo values(102,500); Query OK, 1 row affected (0.16 sec) mysql> insert into CumulativeSumDemo values(103,600); Query OK, 1 row affected (0.16 sec) mysql> insert into CumulativeSumDemo values(104,1000); Query OK, 1 row affected (0.18 sec)
Display all records which I have inserted with the help of insert command. The query is as follows −
mysql> select *from CumulativeSumDemo;
The following is the output −
+--------+-----------+ | BookId | BookPrice | +--------+-----------+ | 101 | 400 | | 102 | 500 | | 103 | 600 | | 104 | 1000 | +--------+-----------+ 4 rows in set (0.00 sec)
To add cumulative sum column, first you need to create a variable. The query is as follows −
mysql> set @CumulativeSum := 0; Query OK, 0 rows affected (0.00 sec)
Implement the above syntax discussed in the beginning to add a cumulative sum column. The query is as follows −
mysql> select BookId,BookPrice,(@CumulativeSum := @CumulativeSum + BookPrice) as CumSum −> from CumulativeSumDemo order by BookId;
The following is the output. Here the cumulative sum column is also visible −
+--------+-----------+--------+ | BookId | BookPrice | CumSum | +--------+-----------+--------+ | 101 | 400 | 400 | | 102 | 500 | 900 | | 103 | 600 | 1500 | | 104 | 1000 | 2500 | +--------+-----------+--------+ 4 rows in set (0.00 sec)
- Related Articles
- How to create a column in an R data frame with cumulative sum?
- How to create a cumulative sum plot in base R?
- Cumulative Sum of a Column in Python Pandas
- How to calculate the cumulative sum / running total of a column in Excel?
- How to create a Cumulative Histogram in Plotly?
- How to sum elements of a column in MySQL?
- How to Find a Cumulative Sum Array in Java?
- How to create NVARCHAR column in MySQL?
- How to create cumulative sum chart with count on Y-axis in R using ggplot2?
- How to sum cells in a column if a condition is met in another column with MySQL?
- Sum the values in a column in MySQL?
- Cumulative percentage of a column in Python Pandas
- How to sum varchar column and display the count in MySQL?
- How to create a table with auto-increment column in MySQL using JDBC?
- SUM a column based on a condition in MySQL
