|
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
--Inquire
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
--result
/ *
class spendtime
-------------------- -----------
A 1
B 3
A 2
B 2
(4 rows affected)
* / |
|