You can use a MySQL implementation as follows:
select id, minus,
(id - coalesce((select sum(minus) from table t2 where t2.id < t.id), 0)) as newId
from table t;
The code idea came from Gordon's post, here is an elaboration.
The first part is select
which states that you want to select certain input from mysql. id, minus
follows, and means that you want to return the values of id
and minus
when the query has finished running.
The rest of it falls under the third part, where you perform the subtraction, and return the value of the subtraction as newId. So when the sql returns, it will get a row with ID, MINUS and NEWID as it's headings.
The subtraction works like this: coalesce returns the first non-null value (i.e. 1
) in the list. the table t2
is using a second version of the table and calling it t2 so that you can compare information.
I hope this clarified things for you!