山東大學數據庫系統實驗七

實驗七

聲明:所有SQL語句均在實驗平臺驗證通過,實驗細節可能隨時間推移老師會進行修改。在此僅提供解答思路,畢竟我的方法肯定不是最優,而且實驗平臺有查重功能,不要一昧的複製哦!

1.在學生表pub.student中統計名字(姓名的第一位是姓氏,其餘爲名字,不考慮複姓)的使用的頻率,將統計結果放入test7_01中。

create table test7_01 as select First_name,frequency from

(

select First_name,count(First_name) frequency from

    (select substr(name,2) First_name from pub.student)

    group by First_name

);

2.在學生表pub.student中統計名字(姓名的第一位是姓氏,不作統計,名字指姓名的第二個之後的漢字)的每個字使用的頻率,將統計結果放入test7_02中。

create table test7_02 as select letter,frequency from

(

select letter,count(letter) frequency from

       (select substr(name,2,1) letter from pub.student union all

       select substr(name,3,1) letter from pub.student)

       group by letter

) where letter is not null;

3.創建“學院班級學分達標情況統計表1”test7_03,依據pub.student, pub.course,pub.student_course統計形成表中各項數據,成績>=60爲及格計入學分,總學分>=10爲達標,院系爲空值的數據不統計在下表中,表結構:院系名稱dname、班級class、學分達標人數p_count1、學分未達標人數p_count2、總人數p_count。

create table test7_03 as

select a.dname,a.class,p_count1,p_count2,p_count from



(select dname,class,count(sid) p_count from pub.student where dname is not null group by dname,class) a



left join



(select dname,class,count(sid) p_count1 from

(

select a.dname,a.class,a.sid,p_count from

(select dname,class,sid from pub.student where dname is not null) a join

(select dname,class,count(sid) p_count from pub.student where dname is not null group by dname,class) b on a.dname = b.dname and a.class = b.class

) dca where dca.sid in

(select sid from

(

select sc.sid,sum(credit) sum_credit from

(

(select sid,cid from pub.student_course where score >= 60) sc join

(select cid,credit from pub.course) c on sc.cid = c.cid

) group by sid

) sa

 where sum_credit>=10) group by dname,class) b on a.dname=b.dname and a.class=b.class



left join



(select dname,class,count(distinct sid) p_count2 from

(

select a.dname,a.class,a.sid,p_count from

(select dname,class,sid from pub.student where dname is not null) a join

(select dname,class,count(sid) p_count from pub.student where dname is not null group by dname,class) b on a.dname = b.dname and a.class = b.class

) dcb where dcb.sid in

(select sid from

(

select sc.sid,sum(credit) sum_credit from

(

(select sid,cid from pub.student_course where score < 60) sc left join

(select cid,credit from pub.course) c on sc.cid = c.cid

) group by sid

) sb

 where sum_credit<10) group by dname,class) c on b.dname=c.dname and b.class=c.class;


update test7_03 set p_count1=0 where p_count1 is null;


update test7_03 set p_count2=p_count-p_count1;

4.創建“學院班級學分達標情況統計表2”test7_04,依據pub.student, pub.course,pub.student_course統計形成表中各項數據,成績>=60爲及格計入學分,2008級及之前的班級總學分>=8爲達標,2008級之後的班級學分>=10未達標,院系爲空值的數據不統計在下表中,表結構:院系名稱dname、班級class、學分達標人數p_count1、學分未達標人數p_count2、總人數p_count。

create table test7_04 as

select a.dname,a.class,p_count1,p_count2,p_count from



(select dname,class,count(sid) p_count from pub.student where dname is not null group by dname,class) a



left join



(select dname,class,count(sid) p_count1 from

(

select a.dname,a.class,a.sid,p_count from

(select dname,class,sid from pub.student where dname is not null) a join

(select dname,class,count(sid) p_count from pub.student where dname is not null group by dname,class) b on a.dname = b.dname and a.class = b.class

) dca where dca.sid in

(select sid from

(

select sc.sid,sum(credit) sum_credit from

(

(select sid,cid from pub.student_course where score >= 60) sc join

(select cid,credit from pub.course) c on sc.cid = c.cid

) group by sid

) sa

 where sum_credit>=10 and class >2008 or sum_credit>=8 and class <=2008) group by dname,class) b on a.dname=b.dname and a.class=b.class



left join



(select dname,class,count(distinct sid) p_count2 from

(

select a.dname,a.class,a.sid,p_count from

(select dname,class,sid from pub.student where dname is not null) a join

(select dname,class,count(sid) p_count from pub.student where dname is not null group by dname,class) b on a.dname = b.dname and a.class = b.class

) dcb where dcb.sid in

(select sid from

(

select sc.sid,sum(credit) sum_credit from

(

(select sid,cid from pub.student_course where score < 60) sc left join

(select cid,credit from pub.course) c on sc.cid = c.cid

) group by sid

) sb

 where sum_credit<10) group by dname,class) c on b.dname=c.dname and b.class=c.class;


update test7_04 set p_count2=p_count-p_count1;

筆記: 

1.實驗七的查詢更爲綜合,例如第一小題,需要先獲得name的姓,然後生成新列First_name,按姓分組後計數,求得同姓人數。第二小題類似。

2.第三小題是求達標,未達標人數。在實際測試中發現:從選課學生中計算得到的達標與未達標人數之和小於等於每個學院的學生人數。這是因爲存在沒有選課的學生,所以未達標人數應該用總人數減去達標人數,否則會出現錯誤。第四小題類似,只需在3的SQL語句上稍加修改where條件即可。

3.其實第3和4小題left join(計算未達標人數的表)完全可以改成一個只有列名的空表,反正計算的結果是有誤的,這裏就不再修改了。

4.比較union與union all的區別,在SQL語句中各種細微關鍵詞的差異往往會得到相差甚大的結果。


ps:本人開通了個人的微信公衆號,希望大家能關注一下,

我會將資源、文章優先推送到公衆號上。

推送自己的一些學習筆記,實驗源代碼等,

歡迎大家互相交流學習。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章