I'm not sure what to call this besides an "accumulated" column.
I have a MySQL table with a column that looks like
+---+
|val|
+---+
| 1 |
| 4 |
| 6 |
| 3 |
| 2 |
| 5 |
+---+
I want to do a query so that I get this column along with another column which is the sum of all the rows from this column so far. In other words, the select would yield
+---+----+
|val| sum|
+---+----+
| 1 | 1 |
| 4 | 5 |
| 6 | 11 |
| 3 | 14 |
| 2 | 16 |
| 5 | 21 |
+---+----+
Does anyone know how I would do this, and whether you can do this in MySQL?
What about
set @running_sum=0
select val, @running_sum:=@running_sum + val from your_table;
I'm new to mysql so take what I say with a grain of salt.
What you want is called a running sum. There are some google results.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With