How to implement cumulative sum in mysql

藏色散人
Release: 2021-12-02 11:06:19
Original
13187 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

##3 30 4 Find the following data

id

money

##1

10

2

20

##40


##id ##30 60 ##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

##3 30 60 4 40 100
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!