gpt4 book ai didi

mysql - 类(class)明智,具有最大总分的学生姓名

转载 作者:搜寻专家 更新时间:2023-10-30 19:57:52 29 4
gpt4 key购买 nike

假设我有一个表如下:

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

即结果包含具有最大总分的类(class)学生姓名。如何为此编写 SQL 查询?例如对于类(class) 1,学生 A 有 70 + 90 = 160,这比 B 和 C 都达到 80 成为最大值。

最佳答案

一种解决方案是计算每个学生在每个类(class)的最高分,并将其用作过滤连接:

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

Live example at SQL Fiddle.

关于mysql - 类(class)明智,具有最大总分的学生姓名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16417266/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com