If you put your data in the form
A B C
a1 b1 b1 - a1 == b1 - a1
a2 b2 c1 - a2 == b1 - a1 - a2
a3 b3 c2 - a3 == b1 - a1 - a2 - a3
a4 b4 c3 - a4 == b1 - a1 - a2 - a3 - a4
...
an bn cn-1 - an == b1 - a1 - a2 - ... - an
you can easily find a solution in analytic functions, for example
select A,
B,
first_value(B) over(order by id) - sum(A) over (order by id) C
from myTable
Please check the actual order (I put order by id)
source
share