From b8b06e3d509892dc55b4a3e4e344ab6b68a38c7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E5=9F=BA=E8=80=80?= <1299433694@qq.com> Date: Thu, 22 Sep 2022 17:22:31 +0800 Subject: [PATCH] 00 --- .../.keep" | 0 .../9.19/.keep" | 0 .../9.19/SQLQuery2.sql" | 43 +++++ ...22\345\272\217\345\207\275\346\225\260.md" | 7 + .../9.21/.keep" | 0 .../2022-9-21\347\254\224\350\256\260.md" | 26 +++ .../9.21/9.21/\344\275\234\344\270\232.sql" | 162 ++++++++++++++++++ 7 files changed, 238 insertions(+) create mode 100644 "41\350\203\241\345\237\272\350\200\200/.keep" create mode 100644 "41\350\203\241\345\237\272\350\200\200/9.19/.keep" create mode 100644 "41\350\203\241\345\237\272\350\200\200/9.19/SQLQuery2.sql" create mode 100644 "41\350\203\241\345\237\272\350\200\200/9.19/\346\216\222\345\272\217\345\207\275\346\225\260.md" create mode 100644 "41\350\203\241\345\237\272\350\200\200/9.21/.keep" create mode 100644 "41\350\203\241\345\237\272\350\200\200/9.21/9.21/2022-9-21\347\254\224\350\256\260.md" create mode 100644 "41\350\203\241\345\237\272\350\200\200/9.21/9.21/\344\275\234\344\270\232.sql" diff --git "a/41\350\203\241\345\237\272\350\200\200/.keep" "b/41\350\203\241\345\237\272\350\200\200/.keep" new file mode 100644 index 0000000..e69de29 diff --git "a/41\350\203\241\345\237\272\350\200\200/9.19/.keep" "b/41\350\203\241\345\237\272\350\200\200/9.19/.keep" new file mode 100644 index 0000000..e69de29 diff --git "a/41\350\203\241\345\237\272\350\200\200/9.19/SQLQuery2.sql" "b/41\350\203\241\345\237\272\350\200\200/9.19/SQLQuery2.sql" new file mode 100644 index 0000000..7638b67 --- /dev/null +++ "b/41\350\203\241\345\237\272\350\200\200/9.19/SQLQuery2.sql" @@ -0,0 +1,43 @@ +select * from AccountInfo +select * from BankCard +select * from CardExchange +select * from CardStateChange +select * from CardTransfer +--1. 关羽的银行卡号为"6225547858741263",查询出余额比关羽多的银行卡信息,示显卡号,身份证,姓名,余额。 +select CardNo 银行卡号,AccountCode 身份证,RealName 姓名,CardMoney 余额 from AccountInfo aa +inner join (select * from BankCard where CardMoney>8000) bb on aa.AccountId=bb.AccountId + + +--2. 从所有账户信息中查询出余额最高的交易明细(存钱取钱信息)。 +select * from CardExchange +where MoneyInBank=any(select max(MoneyInBank) from CardExchange) + + +--3. 查询有取款记录的银行卡及账户信息,显示卡号,身份证,姓名,余额。 +select * from AccountInfo +where AccountId=any( +select AccountId from BankCard +where CardNo=any( +select CardNo from CardExchange +where MoneyOutBank>0 +) +) + + +--4. 查询出没有存款记录的银行卡及账户信息,显示卡号,身份证,姓名,余额。 +select * from AccountInfo +where AccountId=any( +select AccountId from BankCard where CardMoney=0) + + +--5. 关羽的银行卡号为"6225547858741263",查询当天是否有收到转账。 +select * from CardTransfer where CardNoIn ='6225547858741263' +--6. 查询出交易次数(存款取款操作)最多的银行卡账户信息,显示:卡号,身份证,姓名,余额,交易次数。 +select b.CardNo, AccountInfo .AccountCode,AccountInfo .RealName,b.CardMoney,a.cou from BankCard b +inner join (select CardNo,COUNT(*) cou,RANK() over (order by count(*) desc) sort from CardExchange group by CardNo) a on b.CardNo=a.CardNo +inner join AccountInfo on AccountInfo.AccountId=b.AccountId +where a.sort=1 +--7. 查询出没有转账交易记录的银行卡账户信息,显示卡号,身份证,姓名,余额。 +select CardNo,AccountCode,RealName,CardMoney from BankCard b +inner join AccountInfo a on a.AccountId=b.AccountId +where CardNo not in (select CardNoOut from CardTransfer) \ No newline at end of file diff --git "a/41\350\203\241\345\237\272\350\200\200/9.19/\346\216\222\345\272\217\345\207\275\346\225\260.md" "b/41\350\203\241\345\237\272\350\200\200/9.19/\346\216\222\345\272\217\345\207\275\346\225\260.md" new file mode 100644 index 0000000..668834d --- /dev/null +++ "b/41\350\203\241\345\237\272\350\200\200/9.19/\346\216\222\345\272\217\345\207\275\346\225\260.md" @@ -0,0 +1,7 @@ +## 鎺掑簭鍑芥暟 + 197 + 198 **鎺掑簭鍑芥暟 OVER( [鍒嗙粍瀛愬彞] 鎺掑簭瀛愬彞[DESC][ASC] )** + 199 + 200 - ROW_NUMBER()鍑芥暟鐢熸垚鐨勬帓搴忔牴鎹帓搴忓瓙鍙ョ粰鍑**閫掑杩炵画鐨勫簭鍙** + 201 - RANK()鍑芥暟鐢熸垚鐨勬帓搴忔牴鎹帓搴忓瓙鍙ョ粰鍑**閫掑鐨勫簭鍙凤紝浣嗘槸瀛樺湪骞跺垪骞朵笖璺崇┖** + 202 - DENSE_RANK() 鍑芥暟鐢熸垚鐨勬帓搴忔牴鎹帓搴忓瓙鍙ョ粰鍑**閫掑鐨勫簭鍙凤紝浣嗘槸瀛樺湪骞跺垪涓嶈烦绌** \ No newline at end of file diff --git "a/41\350\203\241\345\237\272\350\200\200/9.21/.keep" "b/41\350\203\241\345\237\272\350\200\200/9.21/.keep" new file mode 100644 index 0000000..e69de29 diff --git "a/41\350\203\241\345\237\272\350\200\200/9.21/9.21/2022-9-21\347\254\224\350\256\260.md" "b/41\350\203\241\345\237\272\350\200\200/9.21/9.21/2022-9-21\347\254\224\350\256\260.md" new file mode 100644 index 0000000..6be4ef1 --- /dev/null +++ "b/41\350\203\241\345\237\272\350\200\200/9.21/9.21/2022-9-21\347\254\224\350\256\260.md" @@ -0,0 +1,26 @@ +select * from tb_student + +--鏌ヨ骞撮緞鏈澶х殑瀛︾敓淇℃伅锛岃姹傛樉绀猴細濮撳悕锛屾у埆锛屽勾榫 +select birth,name 濮撳悕,gender 鎬у埆 from tb_student +where birth = (select min(birth) from tb_student) + +--鎺掑簭鍑芥暟 +select birth,濮撳悕,鎬у埆 from ( +select birth,name 濮撳悕,gender 鎬у埆,row_number() over (order by birth ) as birth_rank from tb_student +) as T1 +where T1.birth_rank = 1 + +--鏌ヨ鐢风敓鍜屽コ鐢熶腑骞撮緞鏈澶х殑瀛︾敓淇℃伅锛岃姹傛樉绀猴細濮撳悕锛屾у埆锛屽勾榫 +--闈炲叧鑱斿瓙鏌ヨ +select birth,name 濮撳悕,gender 鎬у埆 from tb_student +where birth in (select min(birth) from tb_student group by gender) + +--鍏宠仈瀛愭煡璇細鍐呴儴鏌ヨ闇瑕佸紩鐢ㄥ閮ㄨ〃宸叉煡璇㈠嚭鐨勪俊鎭 +select birth,name 濮撳悕,gender 鎬у埆 from tb_student t1 +where birth = (select min(birth) from tb_student t2 where t2.gender=t1.gender) + +--绐楀彛鍑芥暟锛氭帓搴 +select birth,濮撳悕,鎬у埆 from ( +select birth,name 濮撳悕,gender 鎬у埆,row_number() over (partition by gender order by birth ) as birth_rank from tb_student +) as T1 +where T1.birth_rank = 1 diff --git "a/41\350\203\241\345\237\272\350\200\200/9.21/9.21/\344\275\234\344\270\232.sql" "b/41\350\203\241\345\237\272\350\200\200/9.21/9.21/\344\275\234\344\270\232.sql" new file mode 100644 index 0000000..ed6544f --- /dev/null +++ "b/41\350\203\241\345\237\272\350\200\200/9.21/9.21/\344\275\234\344\270\232.sql" @@ -0,0 +1,162 @@ +--1. 查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数 + +--1.1 查询同时存在" 01 "课程和" 02 "课程的情况 + +--1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null ) + +--1.3 查询不存在" 01 "课程但存在" 02 "课程的情况 + +--2. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 + +--3. 查询在 SC 表存在成绩的学生信息 + +--4. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +--4.1 查有成绩的学生信息 + +--5. 查询「李」姓老师的数量 + +--6. 查询学过「张三」老师授课的同学的信息 + +--7. 查询没有学全所有课程的同学的信息 + +--8. 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 + +--9. 查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +--10. 查询没学过"张三"老师讲授的任一门课程的学生姓名 + +--11. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + +--12. 检索" 01 "课程分数小于 60,按分数降序排列的学生信息 + + + + +select * from Course +select * from SC +select * from Student +select * from Teacher +--13. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +select * from (select sid,score 课1 from sc where cid=01)aa +left join (select sid,score 课2 from sc where cid=02 ) bb on bb.SId=aa.SId +left join (select sid,score 课3 from sc where cid=03) cc on aa.SId=cc.SId +left join (select sid,avg(score) 平均 from sc group by sid) dd on dd.sid=aa.sid +order by dd.平均 desc + +select aa.sid,ISNULL(课1,0) 课1,ISNULL(课2,0) 课2,ISNULL(课3,0) 课3,平均 from (select sid,score 课1 from sc where cid=01)aa +left join (select sid,score 课2 from sc where cid=02 ) bb on bb.SId=aa.SId +left join (select sid,score 课3 from sc where cid=03) cc on aa.SId=cc.SId +left join (select sid,sum(score)/3 平均 from sc group by sid) dd on dd.sid=aa.sid +order by dd.平均 desc + + +select COUNT(Course.cid) from Course + +select * from sc +right join Course on Course.cid=sc.cid +--14. 查询各科成绩最高分、最低分和平均分: + +--以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + +--要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + +select sc.cid,Course.Cname,max(sc.score) 最高分,min(sc.score) 最低分,avg(sc.score) 平均分, +cast( + CAST( + (SUM(case when score>=60 then 1 else 0 end) * 100.0/COUNT(sc.sid)*1.0) + as float) +as nvarchar(12))+'%' 及格率, +cast( + CAST( + (SUM(case when score>=70 and score>=80 then 1 else 0 end) * 100.0/COUNT(sc.sid)*1.0) + as float) +as nvarchar(12))+'%' 中等率, +cast( + CAST( + (SUM(case when score>=80 and score>=90 then 1 else 0 end) * 100.0/COUNT(sc.sid)*1.0) + as float) +as nvarchar(12))+'%' 优良率, +cast( + CAST( + (SUM(case when score>=90 then 1 else 0 end) * 100.0/COUNT(sc.sid)*1.0) + as float) +as nvarchar(12))+'%' 优秀率, +COUNT(sc.cid) 选修人数 +from sc +join Course on Course.cid=sc.cid +group by sc.cid,Course.Cname +order by 选修人数 desc + + + +--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 + +--17. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + +--18. 查询各科成绩前三名的记录 + +--19. 查询每门课程被选修的学生数 + +--20. 查询出只选修两门课程的学生学号和姓名 + +--21. 查询男生、女生人数 + +--22. 查询名字中含有「风」字的学生信息 + +--23. 查询同名同性学生名单,并统计同名人数 + +--24. 查询 1990 年出生的学生名单 + +--25. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + +--26. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + +--27. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + +--28. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + +--29. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 + +--30. 查询不及格的课程 + +--31. 查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + +--32. 求每门课程的学生人数 + +--33. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + +--34. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成 绩 + +--35. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + +--36. 查询每门功成绩最好的前两名 + +--37. 统计每门课程的学生选修人数(超过 5 人的课程才统计)。 + +--38. 检索至少选修两门课程的学生学号 + +--39. 查询选修了全部课程的学生信息 + +--40. 查询各学生的年龄,只按年份来算 + +--41. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + +--42. 查询本周过生日的学生 + +--43. 查询下周过生日的学生 + +--44. 查询本月过生日的学生 + +--45. 查询下月过生日的学生 \ No newline at end of file -- Gitee