当前位置:趣知科普网>母婴教育>学习交流>

sql 聚合函数和group by 联合使用

学习交流 阅读(2.11W)

很多时候单独使用聚合函数的时候觉得很容易,求个平均值,求和,求个数等,但是和分组一起用就有点混淆了,好记性不如烂笔头,所以就记下来以后看看。

常用聚合函数罗列

(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