• declare @Transaction table

    (

    ID int

    , Store char(3)

    , SKU int

    , AvailableQty int

    , ReceivingStore char(3)

    , QtyDemand int

    )

    insert into @Transaction

    select 1, 'ST1', 100, 20, 'ST2', 14union all

    select 2, 'ST1', 100, 20, 'ST3', 25union all

    select 3, 'ST1', 100, 20, 'ST4', 25

    select ID, Store, SKU, AvailableQty, ReceivingStore, QtyDemand , case when bal < 1 then

    case when QtyDemand + bal < 0 then 0 else QtyDemand + bal end

    else AvailableQty - bal end as transfered

    from

    (

    select *

    , (AvailableQty - SUM(t.QtyDemand ) OVER (Partition by sku ORDER BY ID ROWS UNBOUNDED PRECEDING)) as bal

    from @Transaction t

    ) A

    hope it helps.