SQL 查詢最高分、最低分和平均分語句

來源:互聯網
上載者:User
關鍵字 網路程式設計 資料庫綜合

SQL 查詢最高分、最低分和平均分語句
我們要用就以學生成績為實例吧
/*
結構

學生表
Student(S#,Sname,Sage,Ssex) --S# 學生編號,Sname 學生姓名,Sage 出生年月,Ssex 學生性別
--2.課程表
Course(C#,Cname,T#) --C# --課程編號,Cname 課程名稱,T# 教師編號


*/

查詢各科成績最高分、最低分和平均分:以如下形式顯示:課程ID,課程name,最高分,最低分,平均分,及格率,中等率,優良率,優秀率


--及格為>=60,中等為:70-80,優良為:80-90,優秀為:>=90


--方法1


select m.C# [課程編號], m.Cname [課程名稱],


  max(n.score) [最高分],


  min(n.score) [最低分],


  cast(avg(n.score) as decimal(18,2)) [平均分],


  cast((select count(1) from SC where C# = m.C# and score >= 60)*100.0 / (select count(1) from SC where C# = m.C# ) as decimal(18,2)) [及格率(%)],


  cast((select count(1) from SC where C# = m.C# and score >= 70 and score < 80 )*100.0 / (select count(1) from SC where C# = m.C#) as decimal(18,2)) [中等率(%)],


cast((select count(1) from SC where C# = m.C# and score >= 80 and score < 90 )*100.0 / (select count(1) from SC wher e C# = m.C#) as decimal(18,2)) [優良率(%)],


  cast((select count(1) from SC where C# = m.C# and score >= 90)*100.0 / (select count(1) from SC where C# = m.C# ) as decimal(18,2)) [優秀率(%)]


from Course m , SC n


where m.C# = n.C#


group by m.C# , m.Cname


order by m.C#


--方法2


select m.C# [課程編號], m.Cname [課程名稱],


  (select max(score) from SC where C# = m.C#) [最高分],


  (select min(score) from SC where C# = m.C#) [最低分],


  (select cast(avg(score) as decimal(18,2)) from SC where C# = m.C#) [平均分],


  cast((select count(1) from SC where C# = m.C# and score >= 60)*100.0 / (select count(1) from SC where C# = m.C# ) as decimal(18,2)) [及格率(%)],


cast((select count(1) from SC where C# = m.C# and score >= 70 and score < 80 )*100.0 / (select count(1) from SC wher e C# = m.C#) as decimal(18,2)) [中等率(%)],


  cast((select count(1) from SC where C# = m.C# and score >= 80 and score < 90 )*100.0 / (select count(1) from SC where C# = m.C#) as decimal(18,2)) [優良率(%)],


  cast((select count(1) from SC where C# = m.C# and score >= 90)*100.0 / (select count(1) from SC where C# = m.C# ) as decimal(18,2)) [優秀率(%)]


from Course m


order by m.C#


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.