This is the beauty of Windows Functions, they take a complicated problem and put it in a simple script.
(BTW - I've taken the Sales numbers from here - a great webiste, but I believe it's for PostgresSQL, and then added the year 2016 and the extra numbers. I believe you'll be able to find the pattern.)
CREATE TABLE #temp (StartDate DATE, Sales INT)
INSERT #temp (StartDate, Sales)
VALUES ('20150110', 5)
If you just want the rolling average (you can replace the AVG function with SUM, MIN, MAX as well) for all the time periods, you need to run the following code:
AVG (sales) OVER ( order by StartDate ) AS AVGSales
But if you want to partition by YEAR, for example, you'd better run the following code:
AVG (sales) OVER (Partition by YEAR (StartDAte) order by StartDate ) AS AVGSales
Short & sweet!
(this is a simple example; I need to run this on a total assets for a fund. It's amazing how those extra digits get us confused!)
Don't forget :)
DROP TABLE #temp