Si entiendo correctamente, quiere el precio promedio acumulativo.
Este enfoque utiliza subconsultas para calcular la cantidad total acumulada y el total acumulado pagado. La proporción es el costo promedio:
select t.*, cumepaid / cumeqty as avg_cost
from (select t.*,
(select SUM(qty) from t t2 where t2.item_id = t.item_id and t2.purch_id <= t.purch_id
) as cumeqty,
(select SUM(qty*unit_price) from t t2 where t2.item_id = t.item_id and t2.purch_id <= t.purch_id
) as cumepaid
from t
) t
En SQL Server 2012, puede hacer esto calculando directamente las sumas acumulativas (debería ser más eficiente). También puede hacer esto con cross apply
, pero prefiero SQL estándar.