|
发表于 2020-1-10 23:27: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 'B',1 union all
select 'B',2 union all
select 'A',2 union all
select 'B',2
go
--query
select class,sum(spendtime) as spendtime
from
(select class,spendtime,(select isnull(max(orderindex),0) from #t where orderindex<t.orderindex and class<>t.class) as a from #t t ) tt
group by class,a
--result
A 1
B 6
A 2
B 2 |
|