有一个表B,分别有以下字段:
student_id name address phone sex subject score grade class
1。利用存储过程写出每个学生成绩的总分,和平均分,而且grade='3' ,class='12'.
2。利用存储过程写出男生和女生平均成绩的大小关系。而且求出男女生各门课程的平均分。
sex=1表示男生,sex=0表示女生。
student_id name address phone sex subject score grade class
1。利用存储过程写出每个学生成绩的总分,和平均分,而且grade='3' ,class='12'.
2。利用存储过程写出男生和女生平均成绩的大小关系。而且求出男女生各门课程的平均分。
sex=1表示男生,sex=0表示女生。
Select student_id,Sum(Score) as score
From 表B
Where Grade='3' and class='12'
Group by student_id
--每位学生的平均分
Select student_id,AVG(Score) as score
From 表B
Where Grade='3' and class='12'
Group by student_id
---男生与女生的课程平均分
Select
男生课程平均分=AVG(case when sex=1 then score else 0 end),
女生课程平均分=AVG(case when sex=0 then score else 0 end)
From 表B
begin
Select student_id,sum(Score) as 总,avg(Score) as 平均
From 表B
Where Grade='3' and class='12'
Group by student_id
end
2:
Select
男生课程平均分=AVG(case when sex=1 then score else 0 end),
女生课程平均分=AVG(case when sex=0 then score else 0 end)
From 表B