How to update the following rows after the sum of the previous rows reach a threshold? MySQL
- by Paulo Faria
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?