如何获得每个科目的平均班级列和No. of students提供每个科目的平均列?我创建了以下表格语句和查询。以下是我的查询,该查询为我提供了一个没有班级平均水平和No. of students每个学科的学生的结果。
表格学生有三列,其中包含三个学生
Studentid Firstname Lastname
--------------------------------
1 Oreofeoluwa Ogunkoya
2 Prevailer Adebayo
3 Arike Adeladan
4 Khalilat Yakubu
表课程包含四个课程,Irk和Crk是可选的。
Courseid Course
------------------
1 Maths
2 English
3 Irk
4 Crk
我也有StudentCourse作为课程和学生的联系表。它包含所有学生的分数。
SELECT Course
,Score
,Grade
,Comment
,Pos
,Minimum
,Maximum
FROM (
SELECT S.firstname
,S.lastname
,C.course
,Sc.score
,CASE
WHEN Score BETWEEN 80
AND 100
THEN 'A'
WHEN Score BETWEEN 70
AND 79
THEN 'B'
WHEN Score BETWEEN 60
AND 69
THEN 'C1'
WHEN Score BETWEEN 50
AND 59
THEN 'C2'
WHEN Score BETWEEN 40
AND 49
THEN 'D'
ELSE 'F'
END AS Grade
,CASE
WHEN Score BETWEEN 80
AND 100
THEN 'Excellent'
WHEN Score BETWEEN 70
AND 79
THEN 'Very Good'
WHEN Score BETWEEN 60
AND 69
THEN 'Good'
WHEN Score BETWEEN 50
AND 59
THEN 'Average'
WHEN Score BETWEEN 40
AND 49
THEN 'Pass'
ELSE 'Fail'
END AS Comment
,Rank() OVER (
PARTITION BY course ORDER BY Score DESC
) AS Pos
,Min(Score) OVER (
ORDER BY course
) AS Minimum
,Max(Score) OVER (
ORDER BY course
) AS Maximum
FROM Student S
JOIN Studentcourse Sc ON S.Studentid = Sc.Studentid
JOIN Courses C ON C.courseid = Sc.Courseid
) sub
WHERE firstname = 'Oreofeoluwa'
该查询为我提供了下表,但我需要全班平均水平,没有提供每个学科的学生人数
Course Score Grade Comment Pos Minimum Maximum
---------------------------------------------------
Crk 62.00 C1 Good 1 44.00 62.00
English 80.00 A Excellent 1 43.00 80.00
Maths 96.00 A Excellent 1 36.00 96.00
您必须在课程级别将其分组才能获得课程级别的平均分数。请在下面的示例查询中找到:
Select course,avg(score) as avg_score,count(distinct studentid) as students
from Student S
JOIN Studentcourse Sc ON S.Studentid = Sc.Studentid
JOIN Courses C ON C.courseid = Sc.Courseid
Group by course
如果要将其添加到在学生级别创建的表中,则必须执行左联接。希望这可以帮助。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。