3

希望你们一切都好 我有这样的问题。一个班的学生参加了考试。现在我想选择获得最高分数的前 3 名职位持有者。现在的情况是..如果有两个学生得100分,一个得99分,一个得98分,那么前3名有四个学生

Student1 = 100 marks at first position
Student2 = 100 marks at first position
Student3 = 99 marks at second position
Student4 = 98 marks at thirs position

我怎样才能让这四名学生获得前三个职位

Student1 = 100 marks (who is at first position)
Student2 = 100 marks (who is at first position)
Student3 = 99 marks (who is at second position)
while the student4 = 98 at third poistion is missing.

任何人都可以告诉我选择所有这些学生的正确查询吗?谢谢

4

3 回答 3

5
Select Student,Marks,Rank from (
Select Student,
       Marks,
       Dense_Rank() Over(Order by Marks Desc) as Rank from TableName
    )t 
     where t.Rank<4 
于 2013-07-26T10:49:27.950 回答
0

尝试这样的事情(我不确定 sql 的正确性):

select * from students where students.mark in (select mark from students order by mark DESC LIMIT 3)

此查询需要 3 个最高分,而不是只有这个分数的学生

于 2013-07-26T10:49:53.927 回答
0
 CREATE TABLE BookTitle (
 Id INT NOT NULL,
 NAME VARCHAR(60) NULL,
 Marks INT null
 )
 INSERT INTO BookTitle VALUES (1,'s',100) ,(2,'d',100) ,(3,'e',99),(4,'f',98)

SELECT B.* FROM BookTitle B WHERE 3>=(SELECT COUNT(DISTINCT (A.Marks)) FROM  BookTitle A WHERE A.marks >=B.Marks) 
于 2013-07-26T14:51:03.053 回答