ID | Score |
1 | 10 |
2 | 9 |
3 | 8 |
4 | 7 |
5 | 6 |
6 | 5 |
7 | 4 |
8 | 3 |
9 | 2 |
表格首先是ID升序,Score降序。
目标:找出累计(合计)分数超过50的最大的几个分数,
select * from test where id<=
(
select top 1 t.id from test t
where (select SUM(score) from test t2 where t2.id<=t.id)>50
)
子查询实现的功能是就是超出累计列合计超过50的ID是哪个,然后过虑掉后的ID就可以。