有如下表
id |
money |
1 |
10 |
2 |
20 |
3 |
30 |
4 |
40 |
求出如下数据
id |
money |
cum |
1 |
10 |
10 |
2 |
20 |
30 |
3 |
30 |
60 |
4 |
40 |
100 |
建表
1
2
|
CREATE TABLE cum_demo (id INT ,money INT , PRIMARY KEY (id)) |
插入数据
1
2
|
insert into cum_demo(id,money) values (1,10),(2,20),(3,30),(4.40); |
求累加
1
2
3
4
5
|
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 |
结果
id | money | cum |
1 | 10 | 10 |
2 | 20 | 30 |
3 | 30 | 60 |
4 | 40 | 100 |