# Cumulative subtraction across rows

How can I subtract the Committed value (7) from the IncomingQuantity cumulatively across rows? So that the result would look like:

Thanks!

Gordon Linoff
answered question

### 1 Answer

You need a cumulative sum and some arithmetic:

```
select t.*,
(case when running_iq - incomingquantity >= committed then 0
when running_iq > committed then running_iq - committed
else 0
end) as from_this_row
from (select t2.*, t1.committed,
sum(incomingquantity) over (order by rowid) as running_iq
from table1 t1 cross join
table2 t2
) t;
```

Gordon Linoff
posted this

## Have an answer?

JD