34,594
社区成员
发帖
与我相关
我的任务
分享
declare @t table(ID int,值 int)
insert @t select
1 , 50 union select
2 , 70 union select
3 , 40 union select
4 , 60 union select
5 , 45 union select
6 , 80
select id/2,sum(case when id%2 =1 then 值 else 0 end) - sum(case when id%2 =0 then 值 else 0 end)
from (select id+1 as id ,值 from @t) b
group by id/2
/*
----------- -----------
1 20
2 20
3 35
(所影响的行数为 3 行)
*/
select id/2,sum(case when id%2 =0 then 值 else 0 end) - sum(case when id%2 =1 then 值 else 0 end)
from ta
group by id/2