2

假设我有一个如下表:

Class   |   Subject | Student   | Marks
----------------------------------------
1       |   Maths   |   A       |   70   
1       |   Eng     |   B       |   80
1       |   IT      |   A       |   90 
1       |   IT      |   C       |   80 
2       |   Maths   |   D       |   60   
2       |   Eng     |   E       |   75
2       |   Maths   |   E       |   90 
2       |   IT      |   F       |   80 
3       |   Maths   |   A       |   160   
3       |   Eng     |   B       |   165
3       |   IT      |   G       |   90 

我希望输出为

Class   |   Student     | Marks
----------------------------------------
1       |       A       |   160   
2       |       E       |   165
3       |       B       |   165 

即结果包含班级明智的学生姓名,其总分最高。如何为此编写 SQL 查询?例如,对于第 1 课,学生 A 的分数为 70+90 = 160,超过了 B 和 C 的最大值,均为 80。

4

7 回答 7

5

一种解决方案是计算学生每堂课的最高分,并将其用作过滤连接:

select  ClassStudentSum.*
from    (
        select  class
        ,       student
        ,       sum(Marks) as SumMarks
        from    YourTable
        group by
                class
        ,       student
        ) as ClassStudentSum
join    (
        select  class
        ,       max(SumMarks) as MaxSumMarks
        from    (
                select  class
                ,       student
                ,       sum(Marks) as SumMarks
                from    YourTable
                group by
                        class
                ,       student
                ) ClassStudentSum2
        group by
                class
        ) MaxPerClass
on      MaxPerClass.class = ClassStudentSum.class
        and MaxPerClass.MaxSumMarks = ClassStudentSum.SumMarks

SQL Fiddle 上的实时示例。

于 2013-05-07T11:17:22.210 回答
2

一种更传统(也更慢)的方法......

SELECT x.*
  FROM
     ( SELECT class
            , student
            , SUM(marks) ttl_marks
         FROM yourtable 
        GROUP
           BY class
            , student
     ) x
  LEFT 
  JOIN
     ( SELECT class
            , student
            , SUM(marks) ttl_marks
         FROM yourtable 
        GROUP
           BY class
            , student
     ) y
    ON y.class = x.class
   AND y.ttl_marks > x.ttl_marks
 WHERE y.class IS NULL;
于 2013-05-07T12:22:11.687 回答
1

试试这个查询

查询 1

select a.*, if(@prv=class, 1, 0) as flag, @prv:=class from 
(select class,student, sum(marks) as total from table1 
group by class, student
order by class, total desc)a join (select @prv:=0)tmp
where if(@prv=class, 1, 0) = 0

SQL 小提琴

| CLASS | STUDENT | TOTAL | FLAG | @PRV:=CLASS |
------------------------------------------------
|     1 |       A |   160 |    0 |           1 |
|     2 |       E |   165 |    0 |           2 |
|     3 |       B |   165 |    0 |           3 |

希望这可以帮助

于 2013-05-07T11:36:33.897 回答
1

正确的查询是:

select class, student, sums.mark
from (select class, student, sum(marks) as mark
      from student
      group by class, student
      order by mark desc) sums
group by class
于 2013-05-07T12:38:07.293 回答
0

使用此语句。它是有效的

   select Class,Student,Marks 
from(
    select Class,Student,Marks,Dense_rank() over( partition by class order by marks desc) Rank
      from(Select Class, Student,Max(Marks) Marks 
        from(select Class, Student,Sum(Marks) Marks from Temp14
          group by Class,Student
          order by 1)
        group by Class, Student
        order by 1)
    )
where Rank=1

试试这个。

于 2013-05-07T12:00:08.807 回答
0

我猜这是一个更简单的查询。它工作正常。不需要加入。

SELECT class,
(
SELECT student FROM yourtable
WHERE class = YT.class GROUP BY student
ORDER BY SUM(marks) DESC
LIMIT 1
) AS student,
(
SELECT SUM(marks) FROM yourtable
WHERE class = YT.class GROUP BY student
ORDER BY SUM(marks) DESC
LIMIT 1
) AS marks
FROM yourtable AS YT
GROUP BY class

于 2013-05-08T07:18:48.420 回答
-1
    SQL> with cte as
      2  (select class, student, sum(marks) marks
      3  from engineer
      4  group by class, student
      5  order by class)
      6  select class, student, marks
      7  from (select class, student, marks, dense_rank() over(partition by class order by marks desc) rank
      8  from cte)
      9  where rank=1;

         CLASS S      MARKS
    ---------- - ----------
             1 A        160
             2 E        165
             3 B        165

    SQL>

Here the most important inner query is for cte table. Resulr set created for this one will be as below.

SQL> select class, student, sum(marks)
  2  from engineer
  3  group by class, student
  4  order by class;

     CLASS S SUM(MARKS)
---------- - ----------
         1 A        160
         1 B         80
         1 C         80
         2 D         60
         2 E        165
         2 F         80
         3 A        160
         3 B        165
         3 G         90

9 rows selected.
于 2015-09-23T19:07:12.233 回答