I have a query that return something like this:
| ID | Val |
| 0 | 10 |
| 1 | 20 |
| 2 | 30 |
But instead of that, I want something like this:
| ID | Val | Sum |
| 0 | 10 | 10 |
| 1 | 20 | 30 |
| 2 | 30 | 60 |
Is that a way to do it on the query (I’m using MySQL)?
Tks
This is called cumulative sum.
In
OracleandPostgreSQL, it is calculated using a window function:However,
MySQLdoes not support it.In
MySQL, you can calculate it using session variables:or in a pure set-based but less efficient way: