在不同的表MySQL中按等级划分MAX得分

时间:2016-02-01 19:55:53

标签: mysql group-by sum inner-join

table a
no            name
2001           jon
2002           jonny
2003           mik
2004           mike
2005           mikey
2006           tom
2007           tomo
2008           tommy

table b
code       name      credits     courseCode
A2         JAVA        25           wer
A3         php         25           wer
A4         oracle      25           wer
B2         p.e         50           oth
B3         sport       50           oth
C2         r.e         25           rst
C3         science     25           rst
C4         networks    25           rst

table c
studentNumber     grade      coursecode
2003                68          A2
2003                72          A3   
2003                53          A4
2005                48          A2
2005                52          A3
2002                20          A2
2002                30          A3
2002                50          A4
2008                90          B2
2007                73          B2
2007                63          B3 





 SELECT a.num, a.Fname, 
   b.courseName, b.cMAXscore, b.cCode, c.stuGrade

FROM a
 INNER JOIN c
ON a.no = c.no
 INNER JOIN b
ON c.moduleCode = b.cCode
 INNER JOIN b
ON SUM(b.cMAXscore) / (c.stuGrade)
  AND b.cMAXscore = c.stug=Grade
GROUP BY  a.Fname, b.cMAXscore, b.cCode, b.courseName,c.stuGrade

"计算并显示每个学生姓名(a.Fname)及其身份证号码(a.num)及其等级(c.grade)与coursse名称(b.courseName)和课程最高分数(b.cMAXscoure)。 "

我无法弄清楚如何按年级划分MAX,有人可以帮忙吗?

3 个答案:

答案 0 :(得分:2)

根据规范,它看起来不像聚合函数或GROUP BY是必要的。但规范含糊不清。没有表定义(除了不幸的名称和一些列引用之外)。

表的定义,以及示例数据和所需结果集的示例将大大有助于消除歧义。

基于OP查询中的连接谓词,我建议像这个查询一样,作为一个起点:

SELECT a.Fname
     , a.num
     , c.grade
     , b.courseName
     , b.cMAXsource
  FROM a 
  JOIN c 
    ON c.no = a.no
  JOIN b
    ON b.cCode = c.moduleCode
 ORDER
    BY a.Fname
     , a.num
     , c.grade
     , b.courseName
     , b.cMAXsource

似乎会返回指定的结果(基于我对模糊规范的解释。)如果这不够,即如果不返回所需的结果集,则所需的结果与结果的结果不同这个查询?

(有关您的问题的更多帮助,我建议您使用表格和示例数据设置一个sqlfiddle示例。这将使某人更容易为您提供帮助。)

关注

基于问题中提供的附加信息(表格定义和示例数据......

要获得给定课程的最高(最高)成绩,您可以使用如下查询:

SELECT MAX(c.grade)
  FROM c
 WHERE c.coursecode = 'A2'

获得所有课程的最高分:

SELECT c.coursecode
     , MAX(c.grade) AS max_grade
  FROM c
 GROUP BY c.coursecode
 ORDER BY c.coursecode 

要将每门课程的最高成绩与每个学生成绩相匹配,请将该上一个查询用作另一个查询中的内联视图。像这样:

SELECT g.studentNumber
     , g.grade
     , g.coursecode
     , h.coursecode
     , h.highest_grade
  FROM c g
  JOIN ( SELECT c.coursecode
              , MAX(c.grade) AS highest_grade
           FROM c
          GROUP BY c.coursecode
       ) h
    ON h.coursecode = g.coursecode

要执行计算,您可以在外部查询的SELECT列表中使用表达式。

例如,要将一列的值除以另一列,可以使用除法运算符:

SELECT g.studentNumber  AS student_number
     , g.grade          AS student_grade
     , g.coursecode     AS student_coursecode
     , h.coursecode 
     , h.highest_grade
     , g.grade / h.highest_grade AS `student_grade_divided_by_highest_grade`
  FROM c g
  JOIN ( SELECT c.coursecode
              , MAX(c.grade) AS highest_grade
           FROM c
          GROUP BY c.coursecode
       ) h
    ON h.coursecode = g.coursecode

如果您还要返回学生的姓名,您可以对(不幸的是已命名的)表a执行连接操作。假设studentnumber中的a是唯一的:

  LEFT
  JOIN a
    ON a.studentnumber = c.studentnumber

并在SELECT列表中包含a.Fname AS student_first_name

如果您还需要表b中的列,请同时加入该表。假设coursecode中的b是唯一的:

  LEFT
  JOIN b
    ON b.coursecode = g.courscode

然后可以在SELECT列表中的表达式中引用b.credits

除此之外,您需要更明确一些查询应返回的结果。

如果您正在为学生提供“总体成绩”,则需要指定如何获得该成绩。

答案 1 :(得分:0)

在不知道表格定义的情况下,很难为您的问题提供解决方案。

以下是我要查找的内容的版本:

DECLARE @Student TABLE
(StudentID INT IDENTITY,
 FirstName VARCHAR(255),
 LastName  VARCHAR(255)
);

DECLARE @Course TABLE
(CourseID   INT IDENTITY,
 CourseCode VARCHAR(25),
 CourseName VARCHAR(255),
 MaxScore   INT
);

DECLARE @Grade TABLE
(ID        INT IDENTITY,
 CourseID  INT,
 StudentID INT,
 Score     INT
);
--Student

insert into @Student(FirstName, LastName)   
values ('Test', 'B')


insert into @Student(FirstName, LastName)   
values ('Test123', 'K')


--Course
insert into @Course(CourseCode, CourseName, MaxScore)   
values ('MAT101', 'MATH',100.00)    

insert into @Course(CourseCode, CourseName, MaxScore)   
values ('ENG101', 'ENGLISH',100.00)


--Grade 
insert into @Grade(CourseID, StudentID, Score)  
values (1, 1,93)    

insert into @Grade(CourseID, StudentID, Score)  
values (1, 1,65)

insert into @Grade(CourseID, StudentID, Score)  
values (1, 1,100)


insert into @Grade(CourseID, StudentID, Score)  
values (2, 1,100)   

insert into @Grade(CourseID, StudentID, Score)  
values (2, 1,69)

insert into @Grade(CourseID, StudentID, Score)  
values (2, 1,95)


insert into @Grade(CourseID, StudentID, Score)  
values (1, 2,100)   

insert into @Grade(CourseID, StudentID, Score)  
values (1, 2,65)

insert into @Grade(CourseID, StudentID, Score)  
values (1, 2,100)


insert into @Grade(CourseID, StudentID, Score)  
values (2, 2,100)   

insert into @Grade(CourseID, StudentID, Score)  
values (2, 2,88)

insert into @Grade(CourseID, StudentID, Score)  
values (2, 2,96)


SELECT a.StudentID,
       a.FirstName,
       a.LastName,
       c.CourseCode,
       SUM(b.Score) AS 'StudentScore',
       SUM(c.MaxScore) AS 'MaxCourseScore',
       SUM(CAST(b.Score AS DECIMAL(5, 2))) / SUM(CAST(c.MaxScore AS DECIMAL(5, 2))) AS 'Grade'
FROM @Student a
     INNER JOIN @Grade b ON a.StudentID = b.StudentID
     INNER JOIN @Course c ON c.CourseID = b.CourseID
GROUP BY a.StudentID,
         a.FirstName,
         a.LastName,
         c.CourseCode;

答案 2 :(得分:0)

问题陈述没有说除以最大值,我认为你误解了它。

您需要使用MAXGROUP BY编写一个子查询,以获得每个班级的最高分数。然后,您可以将其与其他表格连接。

SELECT s.name AS student_name, c.name AS course_name, g.grade, m.max_grade
FROM student AS s
JOIN grade AS g ON s.no = g.studentNumber
JOIN course AS c ON c.code = g.courseCode
JOIN (SELECT courseCode, MAX(grade) AS max_grade
      FROM grade
      GROUP BY courseCode) AS m
    ON m.courseCode = c.courseCode

如果您确实需要将等级除以最大值,则可以使用g.grade/m.max_grade