create table #t(a int,b int)
insert into #t select 3,90
insert into #t select 3,76
insert into #t select 2,65
insert into #t select 2,43
insert into #t select 3,32
insert into #t select 2,21
insert into #t select 2,14
insert into #t select 3,42select *,identity(int,1,1) as c into #t1 from #t
select 
    sum_b = sum(b.b) 
from 
    #t1 a,#t1 b 
where 
    (b.c between a.c and (select min(c) from #t1 where c>a.c and a=a.a)) 
    and 
    a.a=3 
    and 
    b.a=2
group by 
    a.cselect 
    max(sum_b)
from
    (select 
         sum_b = sum(b.b) 
     from 
         #t1 a,#t1 b 
     where 
         (b.c between a.c and (select min(c) from #t1 where c>a.c and a=a.a)) 
         and 
         a.a=3 
         and 
         b.a=2
     group by 
         a.c) c