日期:2014-05-17  浏览次数:20694 次

sql基于各个时间段历史数据的平均值,跪求!!!
TYPe WeekEndingOnDate this week
A 2012.08.12 0.998606944444445
B 2012.08.12 0.997755666666667
D 2012.08.12 0.996660555555555
E 2012.08.12 0.999757916666666
G 2012.08.12 0.998900801282053
C 2012.08.12 0.999833333333333
F 2012.08.12 0.998623863636364
G 2012.08.05 0.996723519313306
C 2012.08.05 0.998592857142857
B 2012.08.05 0.999676571428571
D 2012.08.05 0.996521428571429
E 2012.08.05 0.999792142857143
F 2012.08.05 0.998750357142857
A 2012.08.05 0.997293809523809
F 2012.07.29 0.998803571428571
A 2012.07.29 0.989981666666667
E 2012.07.29 0.9998175
G 2012.07.29 0.995505000000001
D 2012.07.29 0.997864285714286
B 2012.07.29 0.999384285714286
C 2012.07.29 0.999138571428571
.......
现有类似上图的表结构及数据,这里提供了3周的数据。

现在希望得到各星期对应的上12周的平均数据,请问怎么办,不想用游标之类的。

如 A 2012.08.12 对应的上12周的数据的平均值就是 2012.08.12前12个星期 A对应值的平均,求高手解答,请详细点啊。










------解决方案--------------------
SQL code

declare @T table (TYPe varchar(1),WeekEndingOnDate datetime,thisweek numeric(16,15))
insert into @T
select 'A','2012.08.12',0.998606944444445 union all
select 'B','2012.08.12',0.997755666666667 union all
select 'D','2012.08.12',0.996660555555555 union all
select 'E','2012.08.12',0.999757916666666 union all
select 'G','2012.08.12',0.998900801282053 union all
select 'C','2012.08.12',0.999833333333333 union all
select 'F','2012.08.12',0.998623863636364 union all
select 'G','2012.08.05',0.996723519313306 union all
select 'C','2012.08.05',0.998592857142857 union all
select 'B','2012.08.05',0.999676571428571 union all
select 'D','2012.08.05',0.996521428571429 union all
select 'E','2012.08.05',0.999792142857143 union all
select 'F','2012.08.05',0.998750357142857 union all
select 'A','2012.08.05',0.997293809523809 union all
select 'F','2012.07.29',0.998803571428571 union all
select 'A','2012.07.29',0.989981666666667 union all
select 'E','2012.07.29',0.9998175 union all
select 'G','2012.07.29',0.995505000000001 union all
select 'D','2012.07.29',0.997864285714286 union all
select 'B','2012.07.29',0.999384285714286 union all
select 'C','2012.07.29',0.999138571428571

select 
    [TYPe],avg(thisweek) as 平均值
from @T 
where WeekEndingOnDate>dateadd(week,-12,getdate())  --最近12周用where 加个条件即可。
group by [TYPe] --求ABCD的分组,把group by 后面改成Type

/*
TYPe 平均值
---- ---------------------------------------
A    0.995294140211640
B    0.998938841269841
C    0.999188253968253
D    0.997015423280423
E    0.999789186507936
F    0.998725930735930
G    0.997043106865120
*/

------解决方案--------------------
SQL code

declare @T table (TYPe varchar(1),WeekEndingOnDate datetime,thisweek numeric(16,15))
insert into @T
select 'A','2012.08.12',0.998606944444445 union all
select 'B','2012.08.12',0.997755666666667 union all
select 'D','2012.08.12',0.996660555555555 union all
select 'E','2012.08.12',0.999757916666666 union all
select 'G','2012.08.12',0.998900801282053 union all
select 'C','2012.08.12',0.999833333333333 union all
select 'F','2012.08.12',0.998623863636364 union all
select 'G','2012.08.05',0.996723519313306 union all
select 'C','2012.08.05',0.998592857142857 union all
select 'B','2012.08.05',0.999676571428571 union all
select 'D','2012.08.05',0.996521428571429 union all
select 'E','2012.08.05',0.999792142857143 union all
select 'F','2012.08.05',0.998750357142857 union all
select 'A','2012.08.05',0.997293809523809 union all
select 'F','2012.07.29',0.998803571428571 union all
select 'A','2012.07.29',0.989981666666667 union all
select 'E','2012.07.29',0.9998175 union all
select 'G','2012.07.29',0.995505000000001 union all
select 'D','2012.07.29',0.997864285714286 union all
select 'B','2012.07.29',0.999384285714286 union all
select 'C','2012.07.29',0.999138571428571

selec