This is calculating a running total. That requires an ordering scheme. Assuming that age column is what you want to order it by, do the following:First, run this query and inspect the results to see if the runningTotal column has the correct values.;WITH cte AS( SELECT a.*,runningTotal FROM YourTable a CROSS APPLY ( SELECT SUM(b.total) AS runningTotal FROM YourTable b WHERE b.age <= a.age ) b)SELECT * FROM cte;
If you are satisfied that the results are correct, then run this query to update the table.;WITH cte AS( SELECT a.*,runningTotal FROM YourTable a CROSS APPLY ( SELECT SUM(b.total) AS runningTotal FROM YourTable b WHERE b.age <= a.age ) b)UPDATE cte SET [sum] = runningTotal;