diff --git "a/18\351\231\210\351\271\217/SQLQuery2.sql" "b/18\351\231\210\351\271\217/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fb5922480c1a859fea2d35c3b149a25040f3a768 --- /dev/null +++ "b/18\351\231\210\351\271\217/SQLQuery2.sql" @@ -0,0 +1,101 @@ +--13. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + + select sid, max(case when Cid ='01'then score else null end)语文, + max(case when cid ='02'then score else null end)数学, + max(case when cid ='03'then score else null end)英语, + AVG(score)平均成绩 + from sc + group by sid + order by avg(score) desc; + + +--14. 查询各科成绩最高分、最低分和平均分: + + select +CId, +(select Cname from Course c where c.CId = sc.CId)课程名称, +MAX(score) 最高分, +MIN(score) 最低分, +AVG(score) 平均分, +( + round( + ( + (select COUNT(*) from SC sc2 where score >= 60 and sc2.CId = sc.CId group by CId) + / + 1.0 + / + (select COUNT(*) from SC sc2 where sc2.CId = sc.CId group by CId) + ),2 + ) +) 及格率, +( + round( + ( + (select COUNT(*) from SC sc2 where score >= 70 and score <= 80 and sc2.CId = sc.CId group by CId) + / + 1.0 + / + (select COUNT(*) from SC sc2 where sc2.CId = sc.CId group by CId) + ),2 + ) +) 中等率, +( + round( + ( + (select COUNT(*) from SC sc2 where score >= 80 and score <= 90 and sc2.CId = sc.CId group by CId) + / + 1.0 + / + (select COUNT(*) from SC sc2 where sc2.CId = sc.CId group by CId) + ),2 + ) +) 优良率, +( + round( + ( + (select COUNT(*) from SC sc2 where score >= 90 and sc2.CId = sc.CId group by CId) + / + 1.0 + / + (select COUNT(*) from SC sc2 where sc2.CId = sc.CId group by CId) + ),2 + ) +) 优秀率 +from SC sc +group by CId + + + + + +--以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + +--要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + +--15. 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + select st.Sname,c.Cname,ROW_NUMBER()over(partition by s.cid order by score desc)排名 from SC s + join Student st on st.SId= s.SId + join Course c on c.CId= s.CId + + + + +--15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 +select s.CId,st.Sname,c.Cname,rank()over(partition by s.cid order by score desc)排名 from SC s + join Student st on st.SId= s.SId + join Course c on c.CId= s.CId + +--16. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + + select a.SId,a.总分,dense_rank() over (order by 总分 desc) 排名 from + (select sid,sum(score) 总分 from sc group by sid ) a + + +--16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + + select a.SId,a.总分,rank() over (order by 总分 desc) 排名 from + (select sid,sum(score) 总分 from sc group by sid ) a; \ No newline at end of file