做练习题时遇到的一问题:
有张考试成绩表ExamTab
(
id int not null,
sex char(4) not null
name char(20) not null
score int not null
)
请问:如何查询出成绩倒4差的考生的姓名以及成绩。
有张考试成绩表ExamTab
(
id int not null,
sex char(4) not null
name char(20) not null
score int not null
)
请问:如何查询出成绩倒4差的考生的姓名以及成绩。
比如:result:
name score
----------
ccc 60
id sex name score
1001 F aa 100
1002 F bb 70
1003 M cc 80
1004 F dd 90
1005 M ee 60
1006 M ff 50
1007 M gg 65
1008 F hh 95
1009 M gg 85
1010 F hh 75
1011 M gg 45
最终结果为(成绩排名倒数第5差的考生):
name score
-----------------
bb 70
insert @t select
1001, 'F', 'aa' , 100
union all select
1002, 'F' , 'bb' , 70
union all select
1003, 'M', 'cc' , 80
union all select
1004, 'F', 'dd' , 90
union all select
1005, 'M', 'ee' , 60
union all select
1006, 'M', 'ff' , 50
union all select
1007, 'M', 'gg' , 65
union all select
1008, 'F', 'hh' , 95
union all select
1009, 'M', 'gg' , 85
union all select
1010, 'F' , 'hh' , 75
union all select
1011, 'M', 'gg' , 45select top 1 * from (select * from @t where id not in (select top 4 id from @t order by score))a order by score
id sex name score
----------- -------------------- -------------------- -----------
1002 F bb 70(所影响的行数为 1 行)第5差