• Just did some quick reading. Didn't find a LAG solution, but windowed functions are awesome for this.

    In the OPs problem at hand, we need something other than ID to ensure sequence of rows. I'm creating one using ROW_NUMBER() but it really should be in the base table

    ; with cte as (select ID, row_number() over(Order by (select null)) as CalcID, SomeValue

    from #sometable)

    ,cte2 as (SELECT ID, CalcID, SomeValue, sum(SomeValue) over(Order by CalcID ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) RunningTotal

    from cte)

    ,cte3 as (SELECT ID, CalcID, SomeValue,RunningTotal, 175-RunningTotal as Remainder

    from cte2)

    select *, case when Remainder > 0 then 0

    when ABS(Remainder) <= someValue then SomeValue + Remainder

    else Somevalue

    end as Newvalue

    from cte3

    Have a great weekend, everyone.

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills