Is this what you want ?create table #temp( t int, a int)insert into #tempselect 1, 300 union allselect 2, 300 union allselect 3, 300 union allselect 4, 600 union allselect 5, 300 union allselect 6, 600 union allselect 7, 1200select t.t, t.a, (select sum(a)/3 from (select top 3 a from #temp x where x.t <= t.t order by t desc) as s) as meanfrom #temp torder by tt a mean ----------- ----------- ----------- 1 300 1002 300 2003 300 3004 600 4005 300 4006 600 5007 1200 700(7 row(s) affected)
----------------------------------'KH'