另一种计算MySQL中每个学生平均点的方法?

时间:2017-04-24 03:25:55

标签: mysql database

我有三张桌子。我想写一份声明,显示学生证,学生姓名和每个学生的平均分。请看表格:

Grade_table

student_ID  courses_names     how many_times         grade
91110010003     MCB                 1                  7
91110010003     THDC                1                  8
91110010003     THVP                1                 9.5
91110010004     MCB                 1                 4.5
91110010004     THDC                1                 5.5
91110010004     THVP                1                 10

课程表

Courses_ID    courses names      period of time
  CSDL          database                30
  MCB          Basic network            30
  THDC        Basic computers           45
  THVP       Concepts of office         45
  XLA           Photoshop               45

学生表

studentID         fullname        gender         DOB            classID
91110010003          A             M          03/03/1993       25CCHT02
91110010004          B             M          08/03/1993       25CCHT02
91110010005          C             M          04/14/1991       25CCHT02
93510010005          D             M          07/27/1993       25CCDH02
93510010008          E             F          03/10/1993       25CCDH02
93510010009          F             M          06/09/1990       25CCDH02
93510010010          G             M          18/12/1989       25CCDH02
99510170307          H             M          05/06/1991       25CCDH02
99510180112          L             M          07/12/1992       25CCDH02
99510180150          M             M          22/03/1993       25CCDH02

1 个答案:

答案 0 :(得分:0)

请尝试以下方法......

SELECT Student_table.studentID,
       Student_table.fullname,
       COALESCE( CAST( AVG( grade ) AS DECIMAL( 11, 2 ) ), 'N/A' ) AS average_grade
FROM Student_table
LEFT JOIN Grade_table ON Student_table.studentID = Grade_table.student_ID
GROUP BY Student_table.studentID;

此声明首先在LEFT JOINStudent_table上对Grade_table的共享值执行student_ID。此练习不需要Courses_table

然后,它将每个学生对结果数据集进行分组,并使用AVG()函数计算每个学生的平均值。如果学生没有Grade_table个条目,则AVG()将返回NULL个值。如果COALESCE()返回非AVG()值,则NULL将返回平均值,如果返回N/A值则返回NULL

如果您有任何问题或意见,请随时发表评论。

附录

我已针对使用以下脚本创建的数据集测试了我的声明...

CREATE TABLE Grade_table
(
    student_ID       LONG,
    courses_names    VARCHAR( 5 ),
    how_many_times   INT,
    grade            DOUBLE
);
INSERT INTO Grade_table ( student_ID,
                          courses_names,
                          how_many_times,
                          grade )
VALUES ( 91110010003, 'MCB',  1, 7 ),
       ( 91110010003, 'THDC', 1, 8 ),
       ( 91110010003, 'THVP', 1, 9.5 ),
       ( 91110010004, 'MCB',  1, 4.5 ),
       ( 91110010004, 'THDC', 1, 5.5 ),
       ( 91110010004, 'THVP', 1, 10 );
CREATE TABLE Student_table
(
    studentID    LONG,
    fullname     VARCHAR( 50 ),
    gender       CHAR( 1 ),
    DOB          DATE,
    classID      CHAR( 8 )
);
INSERT INTO Student_table ( studentID,
                            fullname,
                            gender,
                            DOB,
                            classID )
VALUES ( 91110010003, 'A', 'M', '1993-03-03', '25CCHT02' ),
       ( 91110010004, 'B', 'M', '1993-03-08', '25CCHT02' ),
       ( 91110010005, 'C', 'M', '1991-04-14', '25CCHT02' ),
       ( 93510010005, 'D', 'M', '1993-07-27', '25CCDH02' ),
       ( 93510010008, 'E', 'F', '1993-10-03', '25CCDH02' ),
       ( 93510010009, 'F', 'M', '1990-09-06', '25CCDH02' ),
       ( 93510010010, 'G', 'M', '1989-12-18', '25CCDH02' ),
       ( 99510170307, 'H', 'M', '1991-06-05', '25CCDH02' ),
       ( 99510180112, 'L', 'M', '1992-12-07', '25CCDH02' ),
       ( 99510180150, 'M', 'M', '1993-03-22', '25CCDH02' );