sql 聚合函式和group by 聯合使用

來源:酷知科普網 1.97W

很多時候單獨使用聚合函式的時候覺得很容易,求個平均值,求和,求個數等,但是和分組一起用就有點混淆了,好記性不如爛筆頭,所以就記下來以後看看。

常用聚合函式羅列

(01)AVG() - 返回平均值COUNT() - 返回行數FIRST() - 返回第一個記錄的值LAST() - 返回最後一個記錄的值max() - 返回最大值MIN() - 返回最小值SUM() - 返回總和

建立一張表

(01)CREATE TABLE [dbo].[stuscore]([name] [varchar](50)  ,--學生名字[subject] [varchar](50) ,--課程名字[score] [int] ,--分數[stuid] [int] ,--學號    [d_date] datetime,--登記時間)

(02)新增多條資料insert into [stuscore]select '張三','數學','78','1',GETDATE()union allselect '張三','語文','98','1',GETDATE()union allselect '張三','英語','88','1',GETDATE()union allselect '李四','數學','81','2',GETDATE()union allselect '李四','語文','83','2',GETDATE()union allselect '李四','英語','60','2',GETDATE()

sql 聚合函式和group by 聯合使用

(03)問題羅列:1.    計算每個人的總成績並排名(要求顯示欄位:姓名,總成績)2.    計算每個人的總成績並排名(要求顯示欄位: 學號,姓名,總成績,時間)3.    計算每個人單科的最高成績(要求顯示欄位: 學號,姓名,課程,最高成績)4.    計算每個人的平均成績(要求顯示欄位: 學號,姓名,平均成績)5.    列出各門課程成績最好的學生(要求顯示欄位: 學號,姓名,科目,成績)6.    列出各門課程成績最好的兩位學生(要求顯示欄位: 學號,姓名,科目,成績)7.    統計如下:學號  姓名 語文 數學 英語 總分 平均分8.列出各門課程的平均成績(要求顯示欄位:課程,平均成績)9.列出數學成績的排名(要求顯示欄位:學號,姓名,成績,排名)10.列出數學成績在2-3名的學生(要求顯示欄位:學號,姓名,科目,成績)11.求出李四的數學成績的排名12.統計如下:課程 不及格(0-59)個    良(60-80)個    優(81-100)個

問題解決

(01)計算每個人的總成績並排名(要求顯示欄位:姓名,總成績)select name, SUM(score) as totalscore from stuscore group by name  order by totalscore desc(求和用sum,計算每個人按name 分組,排序order by)

(02)計算每個人的總成績並排名(要求顯示欄位: 學號,姓名,總成績)select stuid,SUM(score) as totalscore,name,d_date from stuscore group by name,stuid ,d_date order by totalscore desc(如果一個表有很多欄位呢?難道也要一個一個欄位羅列出來group by?)select distinct ,d,core,t1.d_date from  stuscore t1,(select stuid,sum(score) as allscore from stuscore group by stuid)t2where d=dorder by core desc

sql 聚合函式和group by 聯合使用 第2張

(03)計算每個人單科的最高成績(要求顯示欄位: 學號,姓名,課程,最高成績) select d,,ect,e from stuscore t1,(select stuid,max(score) as maxscore from stuscore group by stuid) t2where d=d and e=core

sql 聚合函式和group by 聯合使用 第3張

(04)計算每個人的平均成績(要求顯示欄位: 學號,姓名,平均成績)select distinct d,,core from stuscore t1,(select stuid,avg(score) as avgscore from stuscore group by stuid) t2where d=d

sql 聚合函式和group by 聯合使用 第4張

(05)列出各門課程成績最好的學生(要求顯示欄位: 學號,姓名,科目,成績)select distinct d,,e from stuscore t1,(select subject ,max(score) as maxscore from stuscore group by subject) t2where e=core and ect=ect

sql 聚合函式和group by 聯合使用 第5張

(06)統計如下:學號  姓名 語文 數學 英語 總分 平均分select stuid as 學號,name as 姓名, 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) as 總分,(sum(score)/count(*)) as 平均分 from stuscore group by stuid,name  order by 總分 desc

(07)列出各門課程成績最好的兩位學生(要求顯示欄位: 學號,姓名,科目,成績)select * from stuscore t1 where d in (select top 2 d  from stuscore where subject=ect   order by score desc)order by ect

sql 聚合函式和group by 聯合使用 第6張

(08)列出各門課程的平均成績(要求顯示欄位:課程,平均成績)select subject,AVG(score) from stuscore group by subject

(09)列出數學成績的排名(要求顯示欄位:學號,姓名,成績,排名)declare @temp table(pm int identity(1,1),stuid int,name varchar(50),score int)insert into @temp select stuid,name,score from stuscore where subject='數學' order by score descselect * from @temp

sql 聚合函式和group by 聯合使用 第7張

(10)列出數學成績在2-3名的學生(要求顯示欄位:學號,姓名,科目,成績)select t3.*  from(select top 2 t2.*  from (select top 3 name,subject,score,stuid from stuscore where subject='數學'order by score desc) t2 order by e) t3 order by e desc

(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 descdeclare @id intset @id=0;update @tmp set @id=@id+1,pm=@idselect * from @tmp where name='李四'

(12)統計如下:課程 不及格(0-59)個    良(60-80)個    優(81-100)個select subject, (select count(*) from stuscore where score<60 and subject=ect) as 不及格,(select count(*) from stuscore where score between 60 and 80 and subject=ect) as 良,(select count(*) from stuscore where score >80 and subject=ect) as 優from stuscore t1 group by subject

熱門標籤