如何获得mysql中的平均成绩

时间:2014-10-30 05:26:32

标签: mysql sql

我有问题。我不知道如何用文字解释它,但请看下面的代码和结果。

SELECT STUDENT_ID, FNAME, LNAME, MNAME, SUBJECT, GRADE, AVG(GRADE)
AS AVG_GRADE FROM V_STUDENT_GRADES WHERE SCHOOL_YEAR_ID = 2 AND LEVEL_ID = 7
AND PERIOD_ID =1 AND SUBJECT_ID=SUBJECT_ID 
GROUP BY STUDENT_ID, SUBJECT
ORDER BY LNAME, FNAME DESC;

结果:

+------------+------------------------+-------------+--------------------+-------------+-------+-----------+
| STUDENT_ID | FNAME                  | LNAME       | MNAME              | SUBJECT     | GRADE | AVG_GRADE |
+------------+------------------------+-------------+--------------------+-------------+-------+-----------+
|         12 | Roderick               | Almero      | A                  | CIVIL 111   |    75 |        75 |
|         12 | Roderick               | Almero      | A                  | ELECTIVE 1  |    74 |        74 |
|         12 | Roderick               | Almero      | A                  | BIOLOGY     |    87 |        87 |
|         12 | Roderick               | Almero      | A                  | CRIM 111    |    79 |        79 |
|         12 | Roderick               | Almero      | A                  | math 100    |   100 |       100 |
|         12 | Roderick               | Almero      | A                  | CONSTI 222  |    96 |        96 |
|         12 | Roderick               | Almero      | A                  | MAPEH       |    92 |        92 |
|         12 | Roderick               | Almero      | A                  | COMPUTER    |    82 |        82 |
|         12 | Roderick               | Almero      | A                  | ENGLISH 222 |   100 |       100 |
|         13 | Jasima                 | Alonto      | D                  | COMPUTER    |    83 |        83 |
|         13 | Jasima                 | Alonto      | D                  | ENGLISH 222 |    82 |        82 |
|         13 | Jasima                 | Alonto      | D                  | CIVIL 111   |    93 |        93 |
|         13 | Jasima                 | Alonto      | D                  | ELECTIVE 1  |    79 |        79 |
|         13 | Jasima                 | Alonto      | D                  | BIOLOGY     |    87 |        87 |
|         13 | Jasima                 | Alonto      | D                  | CRIM 111    |    72 |        72 |
|         13 | Jasima                 | Alonto      | D                  | math 100    |   100 |       100 |
|         13 | Jasima                 | Alonto      | D                  | CONSTI 222  |    94 |        94 |
|         13 | Jasima                 | Alonto      | D                  | MAPEH       |    98 |        98 |
+----------------------------------------------------------------------------------------------------------+

预期:

+------------+------------------------+-------------+--------------------+-------------+-------+--------------------+
| STUDENT_ID | FNAME                  | LNAME       | MNAME              | SUBJECT     | GRADE | AVG_GRADE          |
+------------+------------------------+-------------+--------------------+-------------+-------+--------------------+
|         12 | Roderick               | Almero      | A                  | CIVIL 111   |    75 |  87.22222222222223 |
|         12 | Roderick               | Almero      | A                  | ELECTIVE 1  |    74 |  87.22222222222223 |
|         12 | Roderick               | Almero      | A                  | BIOLOGY     |    87 |  87.22222222222223 |
|         12 | Roderick               | Almero      | A                  | CRIM 111    |    79 |  87.22222222222223 |
|         12 | Roderick               | Almero      | A                  | math 100    |   100 |  87.22222222222223 |
|         12 | Roderick               | Almero      | A                  | CONSTI 222  |    96 |  87.22222222222223 |
|         12 | Roderick               | Almero      | A                  | MAPEH       |    92 |  87.22222222222223 |
|         12 | Roderick               | Almero      | A                  | COMPUTER    |    82 |  87.22222222222223 |
|         12 | Roderick               | Almero      | A                  | ENGLISH 222 |   100 |  87.22222222222223 |
|         13 | Jasima                 | Alonto      | D                  | COMPUTER    |    83 |  87.55555555555556 |
|         13 | Jasima                 | Alonto      | D                  | ENGLISH 222 |    82 |  87.55555555555556 |
|         13 | Jasima                 | Alonto      | D                  | CIVIL 111   |    93 |  87.55555555555556 |
|         13 | Jasima                 | Alonto      | D                  | ELECTIVE 1  |    79 |  87.55555555555556 |
|         13 | Jasima                 | Alonto      | D                  | BIOLOGY     |    87 |  87.55555555555556 |
|         13 | Jasima                 | Alonto      | D                  | CRIM 111    |    72 |  87.55555555555556 |
|         13 | Jasima                 | Alonto      | D                  | math 100    |   100 |  87.55555555555556 |
|         13 | Jasima                 | Alonto      | D                  | CONSTI 222  |    94 |  87.55555555555556 |
|         13 | Jasima                 | Alonto      | D                  | MAPEH       |    98 |  87.55555555555556 |
+-------------------------------------------------------------------------------------------------------------------+

我想显示具有相同学生ID的每一行中学生的平均分数。

请帮忙。非常感谢。

2 个答案:

答案 0 :(得分:1)

尝试给TROPA ....!

SELECT 
    STUDENT_ID, 
    FNAME, 
    LNAME, 
    MNAME, 
    SUBJECT, 
    GRADE, 
    (SELECT AVG(GRADE) FROM V_STUDENT_GRADES WHERE STUDENT_ID = vsg.STUDENT_ID) AS AVG_GRADE
    FROM V_STUDENT_GRADES vsg
    WHERE SCHOOL_YEAR_ID = 2 AND LEVEL_ID = 7
        AND PERIOD_ID =1 AND SUBJECT_ID=SUBJECT_ID 
        GROUP BY STUDENT_ID, SUBJECT
        ORDER BY LNAME, FNAME DESC;

答案 1 :(得分:0)

试试这个 ` 选择STUDENT_ID,FNAME,LNAME,MNAME,SUBJECT,GRADE,t.AVG_GRADE 从V_STUDENT_GRADES到g 内部联接 ( SELECT STUDENT_ID,AVG(GRADE) AS AVG_GRADE来自V_STUDENT_GRADES WHERE SCHOOL_YEAR_ID = 2 AND LEVEL_ID = 7 AND PERIOD_ID = 1 AND SUBJECT_ID = SUBJECT_ID GROUP BY STUDENT_ID

)t on t.STUDENT_ID = g.STUDENT_ID 在哪里SCHOOL_YEAR_ID = 2 AND LEVEL_ID = 7 AND PERIOD_ID = 1 AND SUBJECT_ID = SUBJECT_ID GROUP BY g.STUDENT_ID,SUBJECT ORDER BY LNAME,FNAME DESC;`