I have a table as: enter image description here

Here I want to subtract the required Qty for a part from On Hand and then the result from the required for next sequence

Hoping to get this as a final result: enter image description here

  • Which version of SQL Server? – scsimon Nov 11 '19 at 22:00

One way would be wiht a window function. You need to partition correctly though.

insert into inventory

   Remaining = OnHand - sum(required) over (partition by Part order by OrderNo)
from inventory

Note, you seem to have miscalculated Part B in your expected results.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.