From 7caad80fb4d80d55ac0f25c8e2643f7307253f2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=88=B4=E4=BF=8A=E9=94=8B?= <379646933@qq.com> Date: Wed, 21 Sep 2022 10:38:29 +0000 Subject: [PATCH 1/2] =?UTF-8?q?2022.9.21=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 戴俊锋 <379646933@qq.com> --- .../2022.9.21\344\275\234\344\270\232.sql" | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 "\346\210\264\344\277\212\351\224\213/\344\275\234\344\270\232/2022.9.21\344\275\234\344\270\232.sql" diff --git "a/\346\210\264\344\277\212\351\224\213/\344\275\234\344\270\232/2022.9.21\344\275\234\344\270\232.sql" "b/\346\210\264\344\277\212\351\224\213/\344\275\234\344\270\232/2022.9.21\344\275\234\344\270\232.sql" new file mode 100644 index 0000000..15c6d95 --- /dev/null +++ "b/\346\210\264\344\277\212\351\224\213/\344\275\234\344\270\232/2022.9.21\344\275\234\344\270\232.sql" @@ -0,0 +1,71 @@ +--13. ƽɼӸߵʾѧпγ̵ijɼԼƽɼ +select sc.SId,c.Cname,score,t.ƽ from sc +inner join Course c on c.cid = sc.CId +inner join +( +select sid,avg(score)ƽ from sc +group by sid +) t +on t.SId = sc.sid + +order by t.ƽ desc +--14. ѯƳɼ߷֡ͷֺƽ֣ +--ʽʾγ IDγ name߷֣ͷ֣ƽ֣ʣеʣʣ +--Ϊ>=60еΪ70-80Ϊ80-90Ϊ>=90 +--Ҫγ̺źѡѯУͬγ̺ +select sc.CId,c.Cname, +(select max(score) from sc sc1 where sc.CId = sc1.CId)߷, +(select min(score) from sc sc1 where sc.CId = sc1.CId)ͷ, +convert(decimal(5,2),(select avg(score) from sc sc1 where sc.cid = sc1.cid))ƽ, +concat(convert(decimal(5,2),(select (count(*)*100)/1.0/(select count(*) from sc sc3 where sc3.cid = sc.cid ) from sc sc2 where sc.cid = sc2.cid and sc2.score >= 60)),'%') , +concat(convert(decimal(5,2),(select (count(*)*100)/1.0/(select count(*) from sc sc3 where sc3.cid = sc.cid ) from sc sc2 where sc.cid = sc2.cid and sc2.score between 70 and 80)),'%') е, +concat(convert(decimal(5,2),(select (count(*)*100)/1.0/(select count(*) from sc sc3 where sc3.cid = sc.cid ) from sc sc2 where sc.cid = sc2.cid and sc2.score between 80 and 90)),'%') , +concat(convert(decimal(5,2),(select (count(*)*100)/1.0/(select count(*) from sc sc3 where sc3.cid = sc.cid ) from sc sc2 where sc.cid = sc2.cid and sc2.score >= 90)),'%') +from sc +inner join Course c +on c.CId = sc.CId +group by sc.CId,c.Cname + + +--15. Ƴɼ򣬲ʾ Score ظʱοȱ +select s.sid,sname,t1.,t2.ѧ,t3.Ӣ from student s +left join +( +select sid,rank() over(order by score desc) from sc where cid = '01' +) t1 on t1.sid = s.sid +inner join +( +select sid,rank() over(order by score desc) ѧ from sc where cid = '02' +) t2 on t2.sid = s.sid +left join +( +select sid,rank() over(order by score desc) Ӣ from sc where cid = '03' +) t3 on t3.sid = s.sid +--15.1 Ƴɼ򣬲ʾ Score ظʱϲ +select s.sid,sname,t1.,t2.ѧ,t3.Ӣ from student s +left join +( +select sid,dense_rank() over(order by score desc) from sc where cid = '01' +) t1 on t1.sid = s.sid +inner join +( +select sid,dense_rank() over(order by score desc) ѧ from sc where cid = '02' +) t2 on t2.sid = s.sid +left join +( +select sid,dense_rank() over(order by score desc) Ӣ from sc where cid = '03' +) t3 on t3.sid = s.sid +--16. ѯѧܳɼܷظʱοȱ +select t.SId,t.ܷ,dense_rank() over (order by ܷ desc) +from +( +select sid,sum(score) ܷ from sc +group by sid +) t +--16.1 ѯѧܳɼܷظʱοȱ +select t.SId,t.ܷ,rank() over (order by ܷ desc) +from +( +select sid,sum(score) ܷ from sc +group by sid +) t \ No newline at end of file -- Gitee From 589cb2541dc3df7c20f149184b03740cb9aa98a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=88=B4=E4=BF=8A=E9=94=8B?= <379646933@qq.com> Date: Wed, 21 Sep 2022 10:39:09 +0000 Subject: [PATCH 2/2] =?UTF-8?q?2022.9.21=E7=AC=94=E8=AE=B0--=E5=85=B3?= =?UTF-8?q?=E8=81=94=E5=AD=90=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 戴俊锋 <379646933@qq.com> --- ...\201\224 \345\255\220\346\237\245\350\257\242.md" | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 "\346\210\264\344\277\212\351\224\213/\347\254\224\350\256\260/2022.9.21--\345\205\263\350\201\224 \345\255\220\346\237\245\350\257\242.md" diff --git "a/\346\210\264\344\277\212\351\224\213/\347\254\224\350\256\260/2022.9.21--\345\205\263\350\201\224 \345\255\220\346\237\245\350\257\242.md" "b/\346\210\264\344\277\212\351\224\213/\347\254\224\350\256\260/2022.9.21--\345\205\263\350\201\224 \345\255\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000..c7d1442 --- /dev/null +++ "b/\346\210\264\344\277\212\351\224\213/\347\254\224\350\256\260/2022.9.21--\345\205\263\350\201\224 \345\255\220\346\237\245\350\257\242.md" @@ -0,0 +1,12 @@ +# 1.关联子查询 + +**(1)先从主查询的表中的列取出第一个值,进入子查询中,得到子查询结果,然后返回父查询,判断父查询的where子句条件,则返回整个语句的第1条结果。** + +**(2)重复上述操作,直到所有主查询中的表中的列记录取完为止。得出整个语句的结果集,就是最后的答案。** + +```sql +--关联子查询:内部查询需要引用外部表已查询出的信息 +select birth,name 姓名,gender 性别 from tb_student t1 +where birth = (select min(birth) from tb_student t2 where t2.gender=t1.gender) +``` + -- Gitee