Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Rolling averages

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-12-07 : 09:32:19
David Mac writes "I'm looking for a way to calculate rolling averages. I assume that this could be done using cursors, but is there an easier way?

For example I have data in a table:


DATE SCORE
1/1/01 20
2/1/01 30
3/1/01 10
4/1/01 0


I want to return a recordset:

DATE SCORE AV
1/1/01 20 20 (20/1)
2/1/01 30 25 (20+25/2)
3/1/01 10 20 etc
4/1/01 0 15


The AV column is the average of the scores of all records up to and including the record in question. Easy to do in Excel!

The table is fairly large, so performance is a real issue.

thanks"

robvolk
Most Valuable Yak

15732 Posts

Posted - 2001-12-07 : 09:52:49
Have you seen this article yet:

http://www.sqlteam.com/item.asp?ItemID=3856

I'm pretty sure one of these techniques can be converted to use AVG() instead of SUM().

Go to Top of Page
   

- Advertisement -