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
 General SQL Server Forums
 New to SQL Server Programming
 moving average

Author  Topic 

dirwin26
Yak Posting Veteran

81 Posts

Posted - 2006-05-22 : 11:37:51
Can anyone tell me how to calculate a moving average for the below table?
--------------------
MONS /vol /counter(the date)
jan /8 / 2006-01-01
feb /9 / etc.
........
oct /5 / etc.
nov /6 /
dec /7 /
--------------------- I am using the below code, but it is having no effect......
SELECT i1.MONS, i1.COUNTER,
( SELECT AVG( i2.VOL )
FROM #RES1 AS i2
WHERE i2.MONS = i1.MONS And
DATEDIFF( month , i2.COUNTER , i1.COUNTER ) Between 0 And 12
) AS MovingAverageOver12Months
FROM #RES1 AS i1
ORDER BY i1.COUNTER

thanks in advance, dirwin

mmarovic
Aged Yak Warrior

518 Posts

Posted - 2006-05-22 : 12:07:53
If I got it right your problem is simalar to running total problem I've written article about. So read http://www.sql-server-performance.com/mm_cursor_friendly_problem.asp and discussion about it. In short, this thing should be handled on the client, but if you insist on sql you'll find a few solutions there.
Go to Top of Page

dirwin26
Yak Posting Veteran

81 Posts

Posted - 2006-05-22 : 12:29:29
thanks, i'll check it out.
Go to Top of Page
   

- Advertisement -