1

I’m trying to calculate a 3 month rolling average grouped by region and month, as in

Region  Month                 Avg(var_a)    Avg(var_b)
Northland   Dec-Jan-Feb       7.1           5.9
Southland   Dec-Jan-Feb       7.2               6.1
Northland   Nov-Dec-Jan       7.4           6.1
Southland   Nov-Dec-Jan       7.5           6.2
Northland   Oct-Nov-Dec       7.5               6.2
Southland   Oct-Nov-Dec       7.5           6.1

Note that month is expanded for illustrative purposes, I’d really expect the output to just say a single month.

Now I can do this by creating a CTE grouping by region and month, then joining to it a couple times like

With month_rollup_cte as
    (Select region,month,sum(var_a) a_sum, sum(var_b) b_sum, count(1) cnt
From vw_score_by_region
    Group by region,month)
Select c1.region, c1.month,sum(c1.a_sum + c2.a_sum + c3.a_sum) / sum(c1.cnt + c2.cnt + c3.cnt) a_avg, sum(c1.b_sum + c2.b_sum + c3.b_sum) / sum(c1.cnt + c2.cnt + c3.cnt) b_avg
From month_rollup_cte c1
Join month_rollup_cte c2 on c1.region = c2. Region and c1.month = dateadd(mm,1,c2.month)
Join month_rollup_cte c3 on c1.region = c3. Region and c1.month = dateadd(mm,2,c3.month)
Group by c1.region, c1.month;

But that’s ugly, imagine if you had to do a 6 month rolling average or 12 month rolling average… I’m trying to use the t-sql 2012 analytic functions, specifically the RANGE option. I’ve used ROWS preceding before, but never range.

What I tried was

select region,avg(var_a) OVER (order by (year(entry_month) * 100 + month(entry_month)) range between 2 preceding and 1 following)    
from [dbo].[vw_score_by_region]
group by region

But I get a syntax error:

*Msg 8120, Level 16, State 1, Line 2
Column 'dbo.vw_score_by_region.month' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.*

Clearly I'm doing something silly, but I'm not sure what.

4

1 回答 1

1

首先,RANGE仅支持UNBOUNDEDCURRENT ROW帧分隔符,它不能与N PRECEDINGN FOLLOWING一起使用。
从你的标题看来,你想得到 3 个月的滚动平均(滑动平均),那么你最好使用ROWS
Using ROWS(这更有可能是你需要的)SQl Fiddle Demo


select region,
       avg(var_a) OVER (partition by region 
                        order by (entry_month) 
                        rows between 2 preceding and current row) as ThreeMonthSlidingAvg  
from [dbo].[vw_score_by_region]

笔记:

不需要计算年+月,如果entry_month是日期或日期时间,已经可以排序了,感谢史蒂夫的指正。
使用范围:

select region,
       avg(var_a) OVER (partition by region,(year(entry_month) * 12 + month(entry_month))/3
order by (entry_month) range between unbounded preceding and current row) as ThreeMonthSlidingAvg
from [dbo].[vw_score_by_region]
注意:使用 RANGE 你必须控制分区宽度,因为你想 agg 3 个月,并且 range 不支持N PRECEDINGN FOLLOWING,它只支持以下:

| 前无界 | 在分区的第一行启动窗口
| 无限关注 | 在分区的最后一行结束窗口
| 当前行 | 在当前行开始或结束窗口
于 2013-03-23T09:39:55.807 回答