select id=identity(int,0,1),value into #tb from 你的表
select a.id/2+1 as value,a.value+b.value as value
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-1
select id=identity(int,0,1),value into #tb from 你的表
select a.id/2+1 as value,a.value+b.value as value
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-1
select b.id/2 as value,a.value+b.value as 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
select b.id/2 as value,a.value+b.value as 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
--1.创建一个合并的函数
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 表
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