如何获得在每个科目中获得最大分数的学生的名字?

时间:2013-08-05 12:28:57

标签: sql sql-server sql-server-2008 sql-server-2005 sql-server-2008-r2

我有下表

 Name  |  Subject  | Marks
 --------------------------
 a        M          20
 b        M          25  
 c        M          30
 d        C          44 
 e        C          45
 f        C          46 
 g        H          20

在这里,我有一张“学生”表,我想得到学生的姓名 来自学生表的每个主题的最大标记,如下面的输出。

 Name | Subject | Marks  
 c        M        30
 f        c        46
 g        h        20

8 个答案:

答案 0 :(得分:4)

您可以使用ROW_NUMBER函数仅返回每个主题的“最佳”行:

SQL Fiddle

MS SQL Server 2008架构设置

CREATE TABLE Student
    ([Name] varchar(1), [Subject] varchar(1), [Marks] int)
;

INSERT INTO Student
    ([Name], [Subject], [Marks])
VALUES
    ('a', 'M', 20),
    ('b', 'M', 25),
    ('c', 'M', 30),
    ('d', 'C', 44),
    ('e', 'C', 45),
    ('f', 'C', 46),
    ('g', 'H', 20)
;

查询1

SELECT Name, Subject, Marks
FROM(
  SELECT *, ROW_NUMBER()OVER(PARTITION BY Subject ORDER BY Marks DESC) rn
    FROM dbo.Student
)X
WHERE rn = 1

<强> Results

| NAME | SUBJECT | MARKS |
--------------------------
|    f |       C |    46 |
|    g |       H |    20 |
|    c |       M |    30 |

答案 1 :(得分:0)

你可以使用其他功能和cte来获得结果..

例如:1

select B.Name,
       A.Subject,
       B.Marks
from ( select Subject,
              max(Marks) as High_Marks
         from Student
       group by Subject
     ) a
  join Student b
    on a.subject = b.subject
   and a.high_Marks = b.Marks

例如:2:使用cte和dense_rank函数

;WITH cte

AS

(

SELECT

   [Name],

   [Subject],

   [Marks],

   dense_rank() over(partition BY [Subject] order by [Marks] DESC) AS Rank

FROM Student

)

SELECT * FROM cte WHERE Rank = 1;

答案 2 :(得分:0)

SQL> with cte as
  2  (
  3  select name, subject, marks, dense_rank() over (partition by subject order
by marks desc) rnk
  4  from student)
  5  select name, subject, marks
  6  from cte
  7  where rnk=1;

N S      MARKS
- - ----------
f c         46
c m         30

SQL>

答案 3 :(得分:0)

SELECT 最高(姓名)作为姓名,主题,最多(标记)作为标记
FROM学生 group by主题

答案 4 :(得分:0)

此基本查询应该适用于您的要求。

SELECT Name, Subject, Max(Marks)
FROM Student
GROUP by Subject;

尝试SQLFiddle

注意:使用SQLite进行检查

答案 5 :(得分:0)

类似问题

编写查询以显示在每个学科中获得最高分的学生的姓名,并按学科名称以升序排列。

如果有多个排行榜,请按字母顺序显示其名称。

将其显示为“科目名称”和“学生名称”。

O / P:

  • 第一列-subject_name
  • 第二列-学生名

enter image description here

此问题的解决方案:

SELECT subject_name,student_name 
    from Student s 
        inner join Mark m on s.student_id=m.student_id
        inner join Subject su on m.subject_id=su.subject_id
        inner join (select subject_id
                          ,max(value) as maximum 
                              from Mark ma group by subject_id
                    ) highmarks 
                        ON highmarks.subject_id=m.subject_id 
                            AND highmarks.maximum=m.value
            order by subject_name,student_name;

答案 6 :(得分:0)

以下查询将正常运行:

select subject_name,student_name from student
inner join mark m using(student_id)
inner join subject su using(subject_id)
inner join (select subject_id,max(value) as maximum from mark m group by subject_id)
highestmark using(subject_id) where highestmark.maximum = m.value
order by subject_name,student_name;

答案 7 :(得分:0)

此查询将起作用

select name,subject,marks from stud where marks in (select max(marks) from stud      group by subject) ;