Home >Database >Mysql Tutorial >How to implement cumulative sum in mysql

How to implement cumulative sum in mysql

藏色散人
藏色散人Original
2021-12-02 11:06:1913350browse

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.

How to implement cumulative sum in mysql

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

##3304Find the following data

id

money

##1

10

2

20

##40


##id##3060 ##40

money

##cum

1
10

10

2
20

30

3

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

##330 60440100
id money cum
1 10 10
2 20 30
##[Related recommendations:

mysql video tutorial

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What database is mysql?Next article:What database is mysql?