diff --git "a/16\345\210\230\346\260\270\346\275\230/\347\254\224\350\256\260/2022-9-18\344\275\234\344\270\232.sql" "b/16\345\210\230\346\260\270\346\275\230/\344\275\234\344\270\232/2022-9-18\344\275\234\344\270\232.sql" similarity index 97% rename from "16\345\210\230\346\260\270\346\275\230/\347\254\224\350\256\260/2022-9-18\344\275\234\344\270\232.sql" rename to "16\345\210\230\346\260\270\346\275\230/\344\275\234\344\270\232/2022-9-18\344\275\234\344\270\232.sql" index aaad1337526775920e7052b421eed211014ab301..db3852ebadc6e6312b511dee92beccd0f2abd622 100644 --- "a/16\345\210\230\346\260\270\346\275\230/\347\254\224\350\256\260/2022-9-18\344\275\234\344\270\232.sql" +++ "b/16\345\210\230\346\260\270\346\275\230/\344\275\234\344\270\232/2022-9-18\344\275\234\344\270\232.sql" @@ -45,4 +45,4 @@ when datepart(YY,PeopleBirth)%12=9 then' when datepart(YY,PeopleBirth)%12=10 then'狗' when datepart(YY,PeopleBirth)%12=11 then'猪' end as 属相 -from people +from people \ No newline at end of file diff --git "a/16\345\210\230\346\260\270\346\275\230/\344\275\234\344\270\232/2022-9-22\344\275\234\344\270\232.sql" "b/16\345\210\230\346\260\270\346\275\230/\344\275\234\344\270\232/2022-9-22\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2049095245ba56d6781e6679f7bd15071af0055a --- /dev/null +++ "b/16\345\210\230\346\260\270\346\275\230/\344\275\234\344\270\232/2022-9-22\344\275\234\344\270\232.sql" @@ -0,0 +1,46 @@ +use TEST04; +select * from Student; +select * from Course; +select * from Teacher; +select * from SC; +--13. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +select st.SId,st.Sname 名字,sc1.score 语文,sc2.score 数学,sc3.score 英语,平均成绩 from Student St +left join SC sc1 on st.SId=SC1.SId and SC1.CId='01' +left join SC sc2 on st.SId=SC2.SId and SC2.CId='02' +left join SC sc3 on st.SId=SC3.SId and SC3.CId='03' +left join ( +select st.SId,AVG(score) 平均成绩 from Student st +left join SC on st.SId=sc.SId +group by st.sid +)a +on st.SId=a.SId +order by 平均成绩 desc + + +select a1.*,avg1 from sc a1 left join +(select sid,avg(score) as avg1 from sc group by sid +)b on a1.SId=b.sid + order by avg1 desc +--14. 查询各科成绩最高分、最低分和平均分: +--以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 +--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 +--要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 +SELECT co.cid AS 课程id ,co.Cname AS 课程,MAX(sc.score) AS 最高分, MIN(sc.score)AS 最低分 ,AVG(sc.score) AS 平均分 , + SUM(CASE WHEN sc.score >= 60 THEN 1 ELSE 0 END)/COUNT(sc.[SId]) AS 及格率, + SUM(CASE WHEN sc.score >= 70 AND sc.score< 80 THEN 1 ELSE 0 END)/COUNT(sc.[SId]) AS 中等率, + SUM(CASE WHEN sc.score >= 80 AND sc.score<90 THEN 1 ELSE 0 END)/COUNT(sc.[SId]) AS 优良率, + SUM(CASE WHEN sc.score >= 90 THEN 1 ELSE 0 END)/COUNT(sc.[SId]) AS 优秀率 +FROM Course co +inner join SC on co.CId=sc.CId +group by co.cid ,co.Cname + +--15. 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 +select *,RANK() over(partition by cid order by score desc) rank from sc +--15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 +select *,DENSE_RANK() over(partition by cid order by score desc) rank from sc +--16. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺 +select sid,sum(score),rank() over(partition by sid order by sum(score) desc) rank from sc +group by sid +--16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 +select sid,sum(score),ROW_NUMBER() over(partition by sid order by sum(score)desc) rank from sc +group by sid \ No newline at end of file diff --git "a/16\345\210\230\346\260\270\346\275\230/\344\275\234\344\270\232/2022-09-18\346\237\245\350\257\242\347\254\224\350\256\260.txt" "b/16\345\210\230\346\260\270\346\275\230/\347\254\224\350\256\260/2022-9-18\347\254\224\350\256\260.txt" similarity index 97% rename from "16\345\210\230\346\260\270\346\275\230/\344\275\234\344\270\232/2022-09-18\346\237\245\350\257\242\347\254\224\350\256\260.txt" rename to "16\345\210\230\346\260\270\346\275\230/\347\254\224\350\256\260/2022-9-18\347\254\224\350\256\260.txt" index 0b3764eda6282c1d3aa0189e7ff09c0016af47cb..dcc5a3c334267e96e259385554865815277c0335 100644 --- "a/16\345\210\230\346\260\270\346\275\230/\344\275\234\344\270\232/2022-09-18\346\237\245\350\257\242\347\254\224\350\256\260.txt" +++ "b/16\345\210\230\346\260\270\346\275\230/\347\254\224\350\256\260/2022-9-18\347\254\224\350\256\260.txt" @@ -192,7 +192,4 @@ SQL璇彞鎵ц椤哄簭: (6) HAVING (9) ORDER BY (10) LIMIT -``` - - - +``` \ No newline at end of file diff --git "a/16\345\210\230\346\260\270\346\275\230/\347\254\224\350\256\260/2022-9-22\347\254\224\350\256\260.txt" "b/16\345\210\230\346\260\270\346\275\230/\347\254\224\350\256\260/2022-9-22\347\254\224\350\256\260.txt" new file mode 100644 index 0000000000000000000000000000000000000000..1218f26af346dbedf47537c344067c3ab582e944 --- /dev/null +++ "b/16\345\210\230\346\260\270\346\275\230/\347\254\224\350\256\260/2022-9-22\347\254\224\350\256\260.txt" @@ -0,0 +1,15 @@ +row_number 涓嶈烦璺冨紡鎺掑簭 +80 1 +78 2 +78 3 +60 4 +rank 璺宠穬寮忔帓搴 +80 1 +78 2 +78 2 +60 4 +danse_rank 杩炵画寮忔帓搴 +80 1 +78 2 +78 2 +60 3 \ No newline at end of file