How to update the following rows after the sum of the previous rows reach a threshold? MySQL
Posted
by
Paulo Faria
on Stack Overflow
See other posts from Stack Overflow
or by Paulo Faria
Published on 2014-06-12T15:20:50Z
Indexed on
2014/06/12
15:24 UTC
Read the original article
Hit count: 203
I want to update the following rows after the sum of the previous rows reach a defined threshold. I'm using MySQL, and trying to think of a way to solve this using SQL only.
Here's an example. Having the threshold 100. Iterating through the rows, when the sum of the previous rows amount >= 100, set the following rows to checked.
Before the operation: | id | amount | checked | | 1 | 50 | false | | 2 | 50 | false | | 3 | 20 | false | | 4 | 30 | false | After the operation: | id | amount | checked | | 1 | 50 | false | | 2 | 50 | false | <- threshold reached (50 + 50 >= 100) | 3 | 20 | true* | | 4 | 30 | true* |
Is it possible to do it with just a SQL query? Do I need a stored procedure? How could I implement it using either solution?
© Stack Overflow or respective owner