Q&A for anime and manga fans
declare @t table(Col1 int, Col2 int) insert into @t values (5, 3), (4, 20), (3, 5), (2, 10), (1, 10) select * from ( select t1.Col1, t1.Col2, SUM(t2.Col2) as runningtotal from @t t1 inner join @t t2 on t1.Col1 >= t2.Col1 group by t1.Col1, t1.Col2 ) ss where runningtotal <= 25 order by col1