Answered by:
Add a cumulative total column to a select statement
Question
Answers

If the temp table is not too big, you can use this set based solution
SELECT acct, Balance, TB.cumBal FROM #Temp1 T cross apply (select sum(Balance) as cumBal from #Temp1 T2 where T1.Date<=T2.Date and T1.Acct = T2.Acct) TB ORDER BY Balance desc, Acct
See also solutions from this long thread
Lightning Fast Hybrid RUNNING TOTAL  Can you slow it down?
Premature optimization is the root of all evil in programming. (c) by Donald Knuth
Naomi Nosonovsky, Sr. ProgrammerAnalyst
My blog Marked as answer by KJian_ Tuesday, November 09, 2010 3:27 AM
All replies

select t1.acct, t1.Balance, (select sum(Balance) from tb t2 where t2.acct <= t1.acct) as cumBal
from #Temp1 t1
order by t1.acct
Ali Hamdar (alihamdar.com  www.ids.com.lb) 
If the temp table is not too big, you can use this set based solution
SELECT acct, Balance, TB.cumBal FROM #Temp1 T cross apply (select sum(Balance) as cumBal from #Temp1 T2 where T1.Date<=T2.Date and T1.Acct = T2.Acct) TB ORDER BY Balance desc, Acct
See also solutions from this long thread
Lightning Fast Hybrid RUNNING TOTAL  Can you slow it down?
Premature optimization is the root of all evil in programming. (c) by Donald Knuth
Naomi Nosonovsky, Sr. ProgrammerAnalyst
My blog Marked as answer by KJian_ Tuesday, November 09, 2010 3:27 AM

Cumulative sum or running total is a sequence of partial sums of a given sequence which is used to display the total sum of data as it grows with time or any other series or progression. In SQL Server, getting running totals in TSQL is not hard task because there are different ways of calculating cumulative sums or running totals.
http://www.sqldatatools.com/2016/12/cumulativesuminsqlserver.html