日期:2014-05-17 浏览次数:20595 次
create table t (a int);
insert into t
select 1
union all
select 2
union all
select 3
union all
select 4;
create table #t (a INT,id int);
insert into #t
select 1,1
union all
select 2,2
union all
select 3,3
union all
select 4,4
SELECT a,'('+RTRIM(a)+'+'+RTRIM((SELECT a FROM #t WHERE id=t1.id-1))+')/2' [avg]
FROM #t t1
create table t (a int);
insert into t
select 1
union all
select 2
union all
select 3
union all
select 4;
;with tt
as
(
select *,ROW_NUMBER() over(order by @@servername) ronum
from t
)
select t1.a,(t1.a + t2.a) /2 'avg(Cn,Cn-1)'
from tt t1
left join tt t2
on t1.ronum = t2.ronum + 1
/*
a avg(Cn,Cn-1)
1 NULL
2 1
3 2
4 3
*/
create table t (a int);
insert into t
select 1
union all
select 2
union all
select 3
union all
select 4;
;with tt
as
(
select *,ROW_NUMBER() over(order by @@servername) ronum
from t
)
select t1.a,(t1.a + t2.a) /2.0 'avg(Cn,Cn-1)'
from tt t1
left join tt t2
on t1.ronum = t2.ronum + 1
/*
a avg(Cn,Cn-1)
1 NULL
2 1.500000
3 2.500000
4 3.500000
*/
create table #tab (a INT,id int);
insert into #tab
select 1,1
union all
select 2,2
union all
select 3,3
union&nb