Skip to content Skip to sidebar Skip to footer

Using "update To A Local Variable" To Calculate Grouped Running Totals

As it appears to be the fasted method, I am trying to use the 'Update to a local variable' method to calculate running totals in SQL. To add an extra layer to this, I am intereste

Solution 1:

The links in the comments allowed me to come up with the right coding. Below is how the update function needs to be modified to group by Day in the above example.

UPDATE @salestbl
SET @RunningTotal = RunningTotal = sales +
    CASE WHEN daycount=@lastday THEN @RunningTotal ELSE 0 END
   ,@lastday=daycount
FROM @salestbl

Post a Comment for "Using "update To A Local Variable" To Calculate Grouped Running Totals"