求教,比方说,有一表,列名分别为Num(组号),id(个数),value;
Num数值是1到10,即10组数据,每组数据里有任意个数的value,现在要求对value进行分组求移动平均,假设求10个value的移动平均,即第1组,求1~10的value的平均值,2~11的平均值,依次类推,求完第一组的移动平均,再求第2组的移动平均。。。。。
------解决方案--------------------
- SQL code
create table tb(num int,id int,va int)insert into tbselect 1,null,1 union allselect 1,null,2 union allselect 1,null,3 union allselect 1,null,4 union allselect 1,null,5 union allselect 2,null,6 union allselect 2,null,7 union allselect 3,null,8 union allselect 3,null,9 union allselect 3,null,1 union allselect 3,null,2 union allselect 3,null,3 union allselect 4,null,4 union allselect 4,null,5 union allselect 4,null,6 union allselect 4,null,7godeclare @cnt intset @cnt = 3;with ach as( select *,px=row_number() over (partition by num order by getdate()) from tb)select num,(px-1)[email protected]+1 as zuhao,cast(avg(va*1.) as decimal(18,2)) as a_value --平均from achgroup by num,(px-1)[email protected]order by numdrop table tb/****************num zuhao a_value----------- -------------------- ---------------------------------------1 1 2.001 2 4.502 1 6.503 1 6.003 2 2.504 1 5.004 2 7.00(7 行受影响)