Home > Database > Mysql Tutorial > body text

How to implement cumulative sum in mysql

藏色散人
Release: 2021-12-02 11:06:19
Original
13299 people have browsed it

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))
Copy after login

Insert data

insert into cum_demo(id,money)
values (1,10),(2,20),(3,30),(4.40);
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template