I hope someone can help with this problem, I have that I am trying to work out a weekly average from the following data example:
Practice ID Cost FiscalWeek
1 10.00 1
1 33.00 2
1 55.00 3
1 18.00 4
1 36.00 5
1 24.00 6
13 56.00 1
13 10.00 2
13 24.00 3
13 30.00 4
13 20.00 5
13 18.00 6
I want to group using the practice identifier, but develop an average value for each practice (more than 500 of them are not only higher) and work every week this week, for example, on week 1 there will not be an average, but week 2 will be on average for weeks 1 and 2, then week 3 will be average for weeks 1, 2 and 3, and then and so on. I need to show this by practice ID and each fiscal week.
At the moment, I have some code that is not very good, and there should be an easier way, this code:
, CTE, case :
CASE WHEN fiscalweek = 1 THEN cost ELSE 0 END AS [1],
CASE WHEN fiscalweek = 2 THEN cost ELSE 0 END AS [2],
CASE WHEN fiscalweek = 3 THEN cost ELSE 0 END AS [3]
1 .. , . 1, 2, 3 .., CTE , , , 6. :
sum([1]) as 'Average Wk 1',
sum([1]+[2])/2 as 'Average Wk 2',
sum([1]+[2]+[3])/3 as 'Average Wk 3',
sum([1]+[2]+[3]+[4])/4 as 'Average Wk 4',
sum([1]+[2]+[3]+[4]+[5])/5 as 'Average Wk 5'
sum([1]+[2]+[3]+[4]+[5]+[6])/6 as 'Average Wk 6'
T-SQL, SSRS. While..Loop, Cursor, .