Home >Database >Mysql Tutorial >How to implement cumulative sum in mysql
Mysql method to implement cumulative sum: 1. Create a data table; 2. Insert data through insert; 3. Through "SELECT a.id,a.money,SUM(lt.money) as cum.. ." statement can be used to implement accumulation.
The operating environment of this article: Windows 7 system, mysql version 5.5, Dell G3 computer.
How does mysql implement cumulative sum?
mysql cumulative sum:
has the following table
id |
money |
##1 | 10 |
2 | 20 |
| 30|
##40 |
money |
##cum | 1 | ||||||||||||||
10
|
10 | 2 | ||||||||||||||
20 | 30 | 3 | ||||||||||||||
60 | 4 |
|||||||||||||||
##100 |
Create table CREATE TABLE cum_demo (id INT,money INT,PRIMARY KEY (id)) Insert data insert into cum_demo(id,money) values (1,10),(2,20),(3,30),(4.40); Find accumulation SELECT a.id,a.money,SUM(lt.money) as cum FROM cum_demo a JOIN cum_demo lt ON a.id >= lt.id GROUP BY a.money ORDER BY id Result
|
The above is the detailed content of How to implement cumulative sum in mysql. For more information, please follow other related articles on the PHP Chinese website!