• The way I'd read the requirement is as follows:-

    Qty is, well, a Quantity.

    Aging Stock is a Capacity

    Priority is the order in which the quantities should be used to fill a capacity.

    The query required is an attempt to front load the qty into the capacity.

    So that total quantity at play here is actually 315 (285 + 30) and the capacity for a given "day" is 305.

    The first record to be returned is 269, 285, 305, 1 because day 269 has a capacity of 305 and a qty of 285.

    The second record to be returned is 269, 20, 305, 2 because day 269 had spare capacity of 20 (305 - 285) which can be dragged forward from day 287.

    The third record to be returned is 287,10,305,2 because day 287 hs a capacity of 305 and a quantity of 10 (after 20 qty was dragged forward to day 269.

    Presumably that pattern should continue through the recordset in priority order.

    I've got to be honest, I've no idea how you'd achieve that in a sql statment and I think you're into the terrirtory of some sort of loop but there are greater minds than I here who might be able to suggest a good way of achieving it.