当前位置:网站首页>Several methods of cumulative summation in MySQL
Several methods of cumulative summation in MySQL
2022-07-22 19:42:00 【xchenhao】
scene
mysql> desc cum_demo;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| money | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.09 sec)
mysql> SELECT * FROM cum_demo ORDER BY id ASC;
+----+-------+
| id | money |
+----+-------+
| 1 | 10 |
| 2 | 20 |
| 3 | 30 |
| 4 | 40 |
| 5 | 20 |
+----+-------+
5 rows in set (0.04 sec)
expect
adopt SQL obtain cum Column ,cum by money Cumulative increase of
+----+-------+-----+
| id | money | cum |
+----+-------+-----+
| 1 | 10 | 10 |
| 2 | 20 | 30 |
| 3 | 30 | 60 |
| 4 | 40 | 100 |
| 5 | 20 | 120 |
+----+-------+-----+
Method
Temporary variable method
SELECT id, money, @cum := money + @cum AS cum
FROM cum_demo, (SELECT @cum := 0) AS t
ORDER BY id ASC;
+----+-------+-----+
| id | money | cum |
+----+-------+-----+
| 1 | 10 | 10 |
| 2 | 20 | 30 |
| 3 | 30 | 60 |
| 4 | 40 | 100 |
| 5 | 20 | 120 |
+----+-------+-----+
join Method
SELECT a.id,a.money,SUM(lt.money) as cum
FROM cum_demo a JOIN cum_demo lt ON a.id >= lt.id
WHERE a.id >= lt.id
GROUP BY a.id
ORDER BY id ASC;
+----+-------+-----+
| id | money | cum |
+----+-------+-----+
| 1 | 10 | 10 |
| 2 | 20 | 30 |
| 3 | 30 | 60 |
| 4 | 40 | 100 |
| 5 | 20 | 120 |
+----+-------+-----+
Window function method
MySQL 8 It can be realized by window function
SELECT id, money, SUM(money) OVER(ORDER BY id ASC) AS cum
FROM cum_demo
ORDER BY id ASC;
+----+-------+-----+
| id | money | cum |
+----+-------+-----+
| 1 | 10 | 10 |
| 2 | 20 | 30 |
| 3 | 30 | 60 |
| 4 | 40 | 100 |
| 5 | 20 | 120 |
+----+-------+-----+
- https://blog.csdn.net/qcyfred/article/details/78045857
- https://www.cnblogs.com/bourneli/p/3248908.html
边栏推荐
- pytest接口自动化测试框架 | 插件二次开发实战
- 问一下,我用flinkCDC读取MySQL的binlog为什么datetime类型字段会早8个小时?
- The problem that double type cannot be accurately calculated
- ATR5179单刀双掷开关芯片替代AS179-92LF
- MySQL 实现累积求和的几种方法
- 消息的可靠性与重复性
- pytest接口自动化测试框架 | 为什么要做pytest插件的二次开发
- 代码覆盖率提示 No code coverage driver is available
- Force deduction solution summary 558- intersection of quadtrees
- 错过等一年!百度超级链数藏发行服务限时五折
猜你喜欢
项目执行过程中有几个关键注意事项?
多重继承
多态性
Explain the adapter technology in NLP from simple to deep
Doctoral application | Professor Ni Zhuoxian of the University of Hong Kong enrolls doctoral students with the full award in the direction of Internet of things and machine learning
STM32中什么是预分频
mysql进阶(十七)Cannot Connect to Database Server问题分析
The strongest screen tool sharex v14.0.1
博士申请 | 香港大学倪卓娴教授招收物联网与机器学习方向全奖博士生
pytest接口自动化测试框架 | 为什么要做pytest插件的二次开发
随机推荐
"Pilot Cup" is coming! Summon the strongest brain in scientific computing, 360000 prize pool waiting for you
multiple inheritance
三层交换机/路由器OSPF配置详解【华为eNSP实验】
C # automatically generates a dictionary (when there is a lot of data)
Neo4j - Cypher 语法示例
Cv520 domestic card reader chip instead of ci520 contactless reader
万兴PDF专家v8.3.8.1253专业版
firewall-cmd 常用操作命令
Si12t touch key chip replaces tms12
Force deduction solution summary 1217- playing chips
威胁狩猎大合集
js map与foreach区别
多态性
Force deduction solution summary 1089- duplicate zero
Pytest interface automated testing framework | plug-in secondary development practice
pytest接口自动化测试框架 | 接口自动化至yaml数据驱动
Force deduction solution summary 1051 height checker
Polymorphism
Definition of derived classes
货币增发为什么会造成通货膨胀? 如何造成