• I edited the main post .

    Sean Lange let me know till if it not clear .. My english is not so good

    Here is the logic:

    select '11003291001-----NOB30000','0V003','A00A00',269,285,305,1 union all

    select '11003291001-----NOB30000','03003','A00A00',287,30,305,2

    output:

    Invtid Siteid WhselocDaysQty

    11003291001-----NOB30000 0V003A00A00269285

    11003291001-----NOB30000 03003 A00A0026920 -- 305-285=20 (20 out of 30)

    11003291001-----NOB30000 03003 A00A0028710 -- 10 out of 30

    output record 1) priority row 1

    output record 2) as per priority row 1 we can store more 20 Qty (from row-1, 305-285=20) , so in input row-2 , we can place 20 out of 30 for row1 days

    output record 3 ) in input record 2 , from qty 30 we placed 20 in row2 so final output row can have 10 Qty