当前位置:网站首页>MySQL 实现累积求和的几种方法
MySQL 实现累积求和的几种方法
2022-07-22 09:54:00 【xchenhao】
场景
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)
期望
通过 SQL 得到 cum 列,cum 为 money 的累积递增
+----+-------+-----+
| id | money | cum |
+----+-------+-----+
| 1 | 10 | 10 |
| 2 | 20 | 30 |
| 3 | 30 | 60 |
| 4 | 40 | 100 |
| 5 | 20 | 120 |
+----+-------+-----+
方法
临时变量法
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 方法
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 |
+----+-------+-----+
窗口函数法
MySQL 8 可通过窗口函数实现
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
边栏推荐
- Delphi提高开发效率之GExperts专家的使用说明
- 7.21 permutation and binary
- The linear layout of fluent fills one line with two controls
- Scope and lifecycle of beans
- Data Lake (18): Flink and iceberg integrate SQL API operations
- MySQL implements querying data from other tables and inserting another table
- Force deduction solution summary 522- longest special sequence II
- Renjie, chief scientist of rongyun: experience produces talents, and career "experience > experience"
- Connectivity of digraph
- What is "real time"
猜你喜欢
MySQL optimization enforces the use of indexes
Don't let fear of marriage kill your happiness!
Flutter's first program Hello world!
Research on the principle of Tencent persistence framework mmkv
2022版Centos8 yum镜像安装&阿里云安装Mysql 5.7教程与问题解决
JS advanced - understanding of functions
Nacos persistent connection MySQL database SM4 encryption scheme
The problem that double type cannot be accurately calculated
Bean 的作用域和生命周期
grafana面板-覆盖字段值
随机推荐
SQL Design Teaching Management Library
2022版Centos8 yum镜像安装&阿里云安装Mysql 5.7教程与问题解决
问一下,我用flinkCDC读取MySQL的binlog为什么datetime类型字段会早8个小时?
Leetcode daily question 2022/3/21-2022/3/27
Delphi中巧用命令行参数实现拖拽文件到程序图标上触发功能
Leetcode daily question 2022/2/28-2022/3/6
Traversing an array with a pointer
自然语言处理NLP文本分类顶会论文阅读笔记(二)
管理的艺术-通过领导力驱动软件研发效能提升
Encryption and decryption of 535 tinyurl
Mail Informer
"35 years old, I retired": This is the most reliable answer to the midlife crisis
「大哉数学之为用」优选法——梯级水库灌溉的优化设计
Preface of daily column
Force deduction solution summary 735 planetary collision
pytest接口自动化测试框架 | pytest安装与规则
Solution to unsuccessful (invalid) password modification of MySQL
Force deduction solution summary 1175 prime number arrangement
软件产业未来发展的几个趋势
Flutter 第一個程序Hello World!