Mysql是学校每个班级中最好的学生

时间:2014-12-13 09:53:20

标签: mysql sql

在MySql中,我需要在termid=10的学校中选择每个班级的优秀学生,以获得下一学期入学的折扣。

请注意,总数不在表格中(我在下面列出清算问题)

我有所有学生工作簿的工作簿表:

id   studentid   classid   exam1    exam2     total   termid    
1       2          11       20        40       60       10        
2       1          22       40        20       60       10        
3       4          11       40        20       60       10        
4       5          33       10        60       70       10        
5       7          22       10        40       50       10        
6       8          11       10        30       40       10        
7       9          33       20        45       65       10        
8       11         11      null      null     null      10        
9       12         54      null      null     null      02 
10      13         58      null      null     null      02

第一个挑战是:exam1exam2VARCHAR,而且总数不在表格中(正如我所解释的那样)。

第二个挑战是:正如你在id=8中看到的那样#std#11没有数字

第三个挑战是:可能是两个学生有顶级水平,所以他们必须在结果中。

我需要结果:

id   studentid   classid   exam1    exam2     total   termid    
1       2          11       20        40       60       10                
3       4          11       40        20       60       10        
4       5          33       10        60       70       10        
2       1          22       40        20       60       10  

我有这个问题但是我提到的工作不好。

SELECT DISTINCT id,studentid,classid,exam1,exam2,total,termid ,(CAST(exam1 AS DECIMAL(9,2))+CAST(exam2 AS DECIMAL(9,2))) FROM workbook  WHERE ClassId = '10';

5 个答案:

答案 0 :(得分:2)

您只需添加值即可获得学生的总数(MySQL会将值转换为数字)。以下是每个类的最大总数:

select w.classid, max(coalesce(w.exam1, 0) + coalesce(w.exam2, 0)) as maxtotal
from workbook w
group by w.classid;

然后,您可以join返回原始数据,以获取有关最佳学生的信息:

select w.*, coalesce(w.exam1, 0) + coalesce(w.exam2, 0) as total
from workbook w join
     (select w.classid, max(coalesce(w.exam1, 0) + coalesce(w.exam2, 0)) as maxtotal
      from workbook w
      group by w.classid
     ) ww
     on w.classid = ww.classid and (coalesce(w.exam1, 0) + coalesce(w.exam2, 0)) = ww.maxtotal;

答案 1 :(得分:0)

使用简单的声明分组:

SELECT 
    studentid,
    classid,
    max(coalesce(exam1,0)) as max_exam_1,
    max(coalesce(exam2,0)) as max_exam_2,
    sum(coalesce(exam1,0) + coalesce(exam2,0)) as sum_exam_total,
    termid
FROM 
    workbook  
WHERE 
    termid=10
GROUP BY 
    1,2
ORDER BY 
    5

答案 2 :(得分:0)

尝试这样的事情:

SELECT id,studentid,classid,exam1,exam2,(CAST(exam1 AS DECIMAL(9,2))+CAST(exam2 AS DECIMAL(9,2))) AS total,termid FROM `workbook` WHERE ((CAST(exam1 AS DECIMAL(9,2))+CAST(exam2 AS DECIMAL(9,2)))) > 50

enter image description here

答案 3 :(得分:0)

另一种方法是将表连接到自身。你找出每个班级的最大值,然后加入这个班级的所有学生,与最高班级相匹配

每个类的最大值(已包含在最终语句中):

SELECT classid, MAX(CAST(exam1 AS UNSIGNED) + CAST(exam2 AS UNSIGNED)) as 'maxtotal'
          FROM students
          WHERE NOT ISNULL(exam1)
                AND NOT ISNULL(exam2)
          GROUP BY classid

完整陈述:

SELECT s2.*, s1.maxtotal
FROM (SELECT classid, MAX(CAST(exam1 AS UNSIGNED) + CAST(exam2 AS UNSIGNED)) as 'maxtotal'
      FROM students
      WHERE NOT ISNULL(exam1)
            AND NOT ISNULL(exam2)
      GROUP BY classid) s1
JOIN students s2 ON s1.classid = s2.classid
WHERE s1.maxtotal = (CAST(s2.exam1 AS UNSIGNED) + CAST(s2.exam2 AS UNSIGNED));

SQL小提琴:http://sqlfiddle.com/#!2/9f117/1

答案 4 :(得分:0)

感谢所有朋友

我认为上面两个答案的结合是最好的:

SELECT s2.*, s1.maxtotal
FROM (SELECT ClassId, MAX(
coalesce(exam1,0)+
coalesce(exam2,0)
) as 'maxtotal'
      FROM workbook
      WHERE
      (
 termid = '11'
      )
      GROUP BY ClassId) s1
JOIN workbook s2 ON s1.ClassId = s2.ClassId
WHERE s1.maxtotal = (
coalesce(exam1,0)+
coalesce(exam2,0)
) AND (s1.maxtotal >'75');

最后一行适用于s1.maxtotal = 0(有些时候没有输入学生成绩,所有等于0所以所有人都会显示为最好的学生)或有时我们需要最低分数(下学期注册)。< / p>

非常感谢

相关问题