?????????
???????????????????????num????????
????--??????
/*
bj         xh   name       cj
---------- ---- ---------- -----------
???         A006 A6         100
???         A005 A5         99
???         A001 A1         89
???         A002 A2         89
????         B001 B7         100
????         B001 B6         99
????         B001 B9         97
????         B001 B8         90
????         B001 B5         88
*/
-- ?????????
declare @student table(
---?????
bj varchar(10)??
-- ??
xh char(4)??
-- ???
name varchar(10)??
-- ????
cj int)
-- ???
declare @tj table(
---????
bj varchar(10)??
-- ??
num int)
-- ????  ?????????????????????num????????
set nocount on
-- ??????????
insert @student select '???' ??'A001'??'A1'??89
insert @student select '???' ??'A002'??'A2'??89
insert @student select '???' ??'A003'??'A3'??59
insert @student select '???' ??'A004'??'A4'??80
insert @student select '???' ??'A005'??'A5'??99
insert @student select '???' ??'A006'??'A6'??100
insert @student select '???' ??'A007'??'A7'??82
insert @student select '????' ??'B001'??'B1'??19
insert @student select '????' ??'B001'??'B2'??81
insert @student select '????' ??'B001'??'B3'??69
insert @student select '????' ??'B001'??'B4'??86
insert @student select '????' ??'B001'??'B5'??88
insert @student select '????' ??'B001'??'B6'??99
insert @student select '????' ??'B001'??'B7'??100
insert @student select '????' ??'B001'??'B8'??90
insert @student select '????' ??'B001'??'B9'??97
insert @tj select '???'??3
insert @tj select '????'??5
?????ο?SQL??
-- 2005.T-SQL
select t.bj??s.xh??s.name??s.cj
from @tj t
cross apply (
SELECT TOP(t.num)
with ties
-- ?? with ties????????4?????2??????е???????
xh??name??cj
from @student
where t.bj=bj
-- ??where ?????????? inner join ???????????? cross join
order by cj desc
)s
order by case when t.bj='???' then 1 else 2 end asc??s.cj desc??s.xh asc
---????