Im trying to generate a running total by month and year. Ive tried a few examples but I cant get it working. This is the sql I have and I would want to create a running total for the totalclients column
Month| Year| TotalClients| Running Total
Jan |2014| 1| 1
Feb| 2014| 4| 5
Mar| 2014| 8| 13
select Month, Year, TotalClients
From Total
This was the code I was trying to use, ive used a declare table as the main data comes from a different query but this should be the bit you need. I also commented out one of the from lines as I was trying out both way, the commented out line was in a few examples on the net but I couldn't get it working
select t1.monthstart, t1.yearstart, t1.TotalClients, sum(t2.TotalClients) as 'RunningTotal'
from @Totals t1 inner join @Totals t2 on t1.monthstart = t2.monthstart and t1.yearstart = t2.yearstart
--from @Totals t1, @Totals t2
WHERE t1.MonthStart <= t2.MonthStart and t1.Yearstart <= t2.Yearstart
GROUP BY t1.Yearstart, t1.MonthStart, t1.TotalClients
ORDER BY t1.yearstart , t1.monthstart