|
发表于 2020-1-10 12:09:02
|
显示全部楼层
create table #t (class varchar(20),spendtime int,orderindex int identity)
insert #t
select 'A',1 union all
select 'B',1 union all
select 'B',2 union all
select 'A',2 union all
select 'B',2
--查询
select ta.class, spendtime=case when ta.orderindex=tb.orderindex+1
then isnull(ta.spendtime,0)+isnull(tb.spendtime,0)
else isnull(ta.spendtime,0) end
from
(
select * from #t a
where not exists (select 1 from #t where class=a.class and orderindex=a.orderindex+1)
) ta
left join
(
select * from #t a
where exists (select 1 from #t where class=a.class and orderindex=a.orderindex+1)
) tb
on ta.class=tb.class
--结果
/*
class spendtime
-------------------- -----------
A 1
B 3
A 2
B 2
(所影响的行数为 4 行)
*/ |
|