TSQL: Calculate the average of the rolling last 4 weeks

Posted by user1917664 on Stack Overflow See other posts from Stack Overflow or by user1917664
Published on 2012-12-20T04:42:40Z Indexed on 2012/12/20 5:03 UTC
Read the original article Hit count: 134

Filed under:
|
|
|

I need your help.

Database: SQL SERVER 2008R2

I want to calculte for a year and a week the average of value of the 4 last weeks.

I have data a table like that:

YEAR WEEKS VALUE

2012 1 3000

2012 2 5000

2012 3 6000

2012 4 7000

2012 5 8000

2012 6 9000

2012 7 1000

2012 8 6000

2012 9 9000

2012 10 4000

And I want that :

YEAR WEEKS VALUE

2012 1 ( Average value for week 49, 50, 51, 52 for the year 2011)

2012 2 ( Average value for week 50, 51, 52 for the year 2011 and week 1 for the year 2012)

2012 3 ( Average value for week 51, 52 for the year 2011 and week 1, 2 for the year 2012)

2012 4 ( Average value for week 52 for the year 2011 and week 1, 2, 3 for the year 2012)

2012 5 5250 -> ( Average value for week 1, 2, 3 , 4 for the year 2012)

2012 6 6500 -> ( Average value for week 2, 3 , 4, 5 for the year 2012)

Thank U for your help

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server