select a.value+b.value
from (
select id,value from 表 where (id-1) % 2 =0
) a inner join (
select id,value from 表 where (id-1) % 2 =1
) b on a.id=b.id-1
from (
select id,value from 表 where (id-1) % 2 =0
) a inner join (
select id,value from 表 where (id-1) % 2 =1
) b on a.id=b.id-1
from your table
group by (id+1)/2
ALTER function f_sum(@id int)
returns int
as
begin
declare @str int
set @str=0
select @str=@str+ value from 表 where id between @id and @id +2 -- 相邻三个
return(@str)
End调用自定义函数得到结果
select distinct id,dbo.f_sum(id) from 表
再对#tmp进行如上操作。
select identity(int,1,1) as newcol ,* into #www from table
from (
select id,value from 表 where (id-1) % 2 =0
) a inner join (
select id,value from 表 where (id-1) % 2 =1
) b on a.id=b.id-1
from (
select id,value from 表 where (id-1) % 2 =0
) a inner join (
select id,value from 表 where (id-1) % 2 =1
) b on a.id=b.id-1
from (
select id,value from #tb where (id) % 2 =0
) a inner join (
select id,value from #tb where (id) % 2 =1
) b on a.id=b.id-1drop table #tb
from (
select id,value from #tb where (id) % 2 =0
) a inner join (
select id,value from #tb where (id) % 2 =1
) b on a.id=b.id-1drop table #tb