Skip to content
Advertisement

mysql Cumulative sum for each unique ID in each sale:

I have trouble adding and accumulating my amounts per id, I use this solution but it doesn’t work and it adds up all the ids,

I use this:

any solution? ineed this

For version 5.7 ?

Advertisement

Answer

You can use a SELECT statement containing window analytic function instead of an UPDATE statement :

acumulado column shouldn’t need to be stored within a table being a computable column.

Demo1

If you still prefer UPDATE, then use :

Demo2

UPDATE : Depending on the last edit consider using

SUM(quantity) OVER (PARTITION BY product_id ORDER BY id) AS acumulado

instead of SUM(quantity) OVER (ORDER BY id) AS acumulado

by adding PARTITION BY product_id in order to distinguish the sums grouped by product_id

Demo3

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement