在MySQL中创建累积总和列 [英] Creating a cumulative sum column in MySQL
本文介绍了在MySQL中创建累积总和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例表ID:(num是键,因此不会重复)
Sample table ID: (num is a key so there wouldn't be any duplicates)
num
1
5
6
8
2
3
所需输出:
(应排序并具有累加总和列)
Desired output:
(Should be sorted and have a cumulative sum column)
num cumulative
1 1
2 3
3 6
5 11
6 17
8 25
这是我得到的一种解决方案:
This is one solution I got:
select a.num, sum(b.num) from ID a, ID b where b.num <= a.num group by a.num order by a.num;
推荐答案
我想我已经找到解决方案了.
I think I figured out the solution.
Select num as n,
(select sum(num) from ID where num <= n)
from ID order by n;
这篇关于在MySQL中创建累积总和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文