stored procedures - MySQL query that computes partial sums -
Which query should I perform in the MySQL database to get results with partial amounts of the source table?
For example, when I have a table:
id | Val 1 1 2 | 2 3 | 3 4 4
I want to get results like this:
Id | Val 1 | 1 2 | 3 # 1 + 2 3 | 6 # 1 + 2 + 3 4 | 10 # 1 + 2 + 3 + 4
Now I get this result with the cursed stored procedure and with loops I want to find a better way to do this.
You can do this by joining the table SOM itself by adding all the rows to this line Will give:
select cur.id, sum (prev.val) is left on the curtid except TheTable TheTable prev.id & gt; = Prev.id by group cur.id
MySQL also allows users to use the variable to calculate, which is more efficient but some of a hack is considered:
select id, @ running_total: = @running_total + val runningTotal from TheTable
Comments
Post a Comment