I asked a similar question recently, but the actual implementation proved more difficult as it had to rely on a date field, not an ID field, so I'm going to start a new question as the method will no doubt prove somewhat different.
Looking at the table below, only those rows whose SUM(PurchQty) are necessary to arrive at a value greater than / equal to "CurrentStock" should be returned, and those must be newest rows first, based on date. This needs to be evaluated and row(s) returned per "ProductCode".
Therefore, given this table:
ID ProductCode Date PurchQty CurrentStock
1001 AB101 14/12/2016 9 14
1111 AB101 01/01/2017 18 14
1223 AB101 15/01/2017 20 14
1233 BB400 02/01/2017 50 40
1321 AB101 31/01/2017 8 14
1400 BB400 12/12/2016 90 40
1456 CC200 13/03/2017 100 20
Our query should yield the result:
ProductCode Date PurchQty CurrentStock
AB101 31/01/2017 8 14
AB101 15/01/2017 20 14
BB400 02/01/2017 50 40
CC200 13/03/2017 100 20
(note these are UK dates - dd/mm/yyyy - but that is unimportant)