日期:2014-05-17 浏览次数:20795 次
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
*/
------解决方案--------------------
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