2

我的 SQL 表中有年度数据(按天计算)。我想计算 3 个月的滚动/移动平均值。如何做到这一点?

4

1 回答 1

0

也许这会给你一个建议。

drop table #temp

;with cte as
(
    select 1 as id
    union all
    select id + 1 from cte
    where id < 100
)
select 
    *, id / 10 as "year", null as "rolSum"
into #temp
from 
    cte 
where 
    id < 100

select "year", id from #temp order by "year"


declare @rolSum int = 0
update #temp set @rolSum = "rolSum" = @rolSum + id from #temp

select year, id, rolSum, avg( id * 1.0 ) over ( partition by "year" order by id ) from #temp

rolSumavg(...)如果不满足您的需求,则仅包含在内以进行进一步处理。

于 2013-09-20T12:37:04.157 回答