-2

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 at 22:00
0

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

insert into inventory
values
('A',1,10,3),
('A',2,10,2),
('B',1,5,3),
('B',2,5,2),
('B',3,5,2)

select 
   *, 
   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.

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.