學生各門課程成績統計SQL語句大全(面試題)

来源:http://www.cnblogs.com/lsgcoder101/archive/2016/10/29/6011059.html
-Advertisement-
Play Games

創建表 插入數據 查詢結果顯示,如下截圖: 問題: 1.計算每個人的總成績併排名(要求顯示欄位:姓名,總成績) select name,SUM(score) as allscore from dbo.stuscore group by name order by allscore; View Cod ...


創建表

SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[stuscore]
( [name] [varchar](50)   COLLATE Chinese_PRC_CI_AS   NULL,    
[subject] [varchar](50)   COLLATE Chinese_PRC_CI_AS   NULL,    
[score] [int]   NULL,   
[stuid] [int]    NULL) 
ON [PRIMARY] 
GO
SET ANSI_PADDING OFF

插入數據

insert into dbo.stuscore values ('張三','數學',89,1);
insert into dbo.stuscore values ('張三','語文',80,1);
insert into dbo.stuscore values ('張三','英語',70,1);
insert into dbo.stuscore values ('李四','數學',90,2);
insert into dbo.stuscore values ('李四','語文',70,2);
insert into dbo.stuscore values ('李四','英語',80,2);

查詢結果顯示,如下截圖:

問題:   1.計算每個人的總成績併排名(要求顯示欄位:姓名,總成績) 
select name,SUM(score) as allscore from dbo.stuscore
group by name 
order by allscore;
View Code

2.計算每個人的總成績併排名(要求顯示欄位: 學號,姓名,總成績) 

select stuid,name,SUM(score) as allscore from dbo.stuscore 
group by name,stuid 
order by allscore;
View Code 3.計算每個人單科的最高成績(要求顯示欄位: 學號,姓名,課程,最高成績)
select t1.stuid,t1.name,t1.subject,t1.score from stuscore t1,
(select stuid,max(score) as maxscore from stuscore group by stuid) t2 
where t1.stuid=t2.stuid and t1.score=t2.maxscore;
View Code 4.計算每個人的平均成績(要求顯示欄位: 學號,姓名,平均成績)
select stuid,name,AVG(score) avgscore from dbo.stuscore 
group by stuid,name;
View Code 5.列出各門課程成績最好的學生(要求顯示欄位: 學號,姓名,科目,成績) 
select t1.stuid,t1.name,t1.subject,t1.score from stuscore t1,(
select subject,MAX(score) as maxscore from stuscore group by subject)t2
where t1.subject = t2.subject and t1.score = t2.maxscore;
View Code 6.列出各門課程成績最好的兩位學生(要求顯示欄位: 學號,姓名,科目,成績) 
select  t1.* from stuscore t1 where t1.stuid in (
select top 2 stuid from stuscore where subject = t1.subject order by score desc)
order by t1.subject;
View Code 7.統計如下: 
學號 姓名 語文 數學 英語 總分 平均分
             
select stuid 學號,name 姓名,sum(case when subject='語文' then score else 0 end )as 語文,
sum(case when subject='數學' then score else 0 end )as 數學,
sum(case when subject='英語' then score else 0 end )as 英語,
SUM(score)總分,avg(score)平均分 from stuscore
group by stuid,name order by 總分;
View Code   8.列出各門課程的平均成績(要求顯示欄位:課程,平均成績)
select subject,AVG(score)平均成績 from stuscore 
group by subject;
View Code   9.列出數學成績的排名(要求顯示欄位:學號,姓名,成績,排名)
select stuid,name,score,
(select count(*) from stuscore t1 where subject ='數學' and t1.score > t2.score)+1 as 名次 from stuscore t2  
where subject='數學' order by score desc;
 --註釋:排序,比較大小,比較的次數+1 = 排名。
View Code   10.列出數學成績在2-3名的學生(要求顯示欄位:學號,姓名,科目,成績) 
select t3.* from (
select top 2  t2.* from (
select top 3 stuid,name,subject,score from stuscore where 
subject = '數學' order by score desc) t2 order by t2.score) t3
order by t3.score desc;
View Code
 select t3.*  from (
 select top 100 percent stuid,name,subject,score,
(select count(*) from stuscore t1 where subject ='數學' and t1.score > t2.score)+1 as 名次 from
 stuscore t2  where subject='數學' order by t2.score desc) t3 
 where t3.名次 between 2 and 3 order by t3.score desc;
View Code
 select t3.*  from (
 select stuid,name,subject,score,
(select count(*) from stuscore t1 where subject ='數學' and t1.score > t2.score)+1 as 名次 from
 stuscore t2  where subject='數學') t3 
 where t3.名次 between 2 and 3 order by t3.score desc;
View Code

後面兩個方法的不同之處可以參見:http://blog.csdn.net/wrm_nancy/article/details/17170115

11.求出李四的數學成績的排名 
declare @tmp table(pm int,name varchar(50),score int,stuid int)insert into @tmp 
select null,name,score,stuid from stuscore where subject='數學' 
order by score desc declare @id int set @id=0;
update @tmp set @id=@id+1,pm=@id select * from @tmp where name='李四'
View Code
select stuid,name,subject,score,(select count(*) from stuscore t1 where subject ='數學' and t1.score > t2.score)+1 as 名次
 from stuscore t2  where subject='數學' and name = '李四' order by score desc;
View Code 12.統計如下: 
課程 不及格(0-59)個 良(60-80)個 優(81-100)個
       
 select subject 科目,sum(case when score between 0 and 59 then 1 else 0 end) as 不及格,
 sum(case when score between 60 and 80 then 1 else 0 end) as 良,
 sum(case when score between 81 and 100 then 1 else 0 end) as 優秀 from stuscore
 group by subject;
View Code 13.統計如下:   數學: 張三(50分),李四(90分),王五(90分),趙六(76分)   
 declare @s nvarchar(1000)
 set @s=''
 select @s =@s+','+name+'('+convert(nvarchar(10),score)+'分)' from 
 stuscore where subject='數學'
 set @s=stuff(@s,1,1,' ')print '數學:'+@s
View Code

 

 
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 前幾天將我的Xcode升到了8,但是在運行程式時,會列印很多沒有用的信息,如下圖: Xcode8運行程式時列印的亂碼 Xcode8運行程式時列印的亂碼 於是各種尋求答案,找到如下答案: Edit Scheme-> Run -> Arguments, 在Environment Variables裡邊添 ...
  • 本文是轉載的,轉載地址: "大白話解釋Strategy模式和State模式的區別" 先上圖: 本質上講,策略模式和狀態模式做得是同一件事:去耦合。怎麼去耦合?就是把乾什麼(語境類)和怎麼乾(策略介面)分開,互不依賴。打個比方,下麵是我一天的行程: 但問題來了,啪啪啪是個技術活,有著名的48式,今天到 ...
  • 如今,隨著信息技術的不斷發展,很多公司採用微信企業號來進行企業與員工之間的聯繫。其實微信企業號中右很多獨立的應用。 那麼如何可以將報表系統集成到微信中呢?這裡分享一下在微信企業號中創建獨立的報表應用,並且將微信賬號單點登錄到帆軟報表軟體FineReport的許可權對接。 ...
  • 現在大部分應用程式都把業務邏輯處理,數據調用等功能封裝成了服務的形式,應用程式只需要調用這些web服務就好了,在這裡就不贅述web服務的優點了。本文總結如何在android中調用Web服務,通過傳遞基類型和複雜類型對比調用.NET平臺發佈的WCF服務和WebService服務之間的區別。 0 寫在前 ...
  • 前言 學習ios這幾天來,總結下,函數的定義,調用。跟其他語言都有一定的區別; 幾個特別重要的就是對象的迭代的使用和判斷、取隨機數、動畫的實現及數組的深入研究等等 之前的總結地址 ios開發 學習積累20161024~20161026: http://www.cnblogs.com/jasonxu1 ...
  • 鏈接 ...
  • 前言 學習本系列內容需要具備一定 HTML 開發基礎,沒有基礎的朋友可以先轉至 "HTML快速入門(一)" 學習 本人接觸 React Native 時間並不是特別長,所以對其中的內容和性質瞭解可能會有所偏差,在學習中如果有錯會及時修改內容,也歡迎萬能的朋友們批評指出,謝謝 文章第一版出自簡書,如果 ...
  • 1.添加載入更多佈局 1_初始化和隱藏代碼在RefreshListView構造方法中調用 2_佈局文件refresh_listview_footer.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android= ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...