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

 时间:2024-10-16 17:26:19

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

常用聚合函数罗列

1、AVG() - 返回平均值COUNT() - 返回行数FIRST() - 返回第一个记录的值LAST() - 返回最后一个记录的值MAX() - 返回最大值MIN() - 返回最小值SUM() - 返回总和

创建一张表

1、CREATE TABLE [dbo].[stuscore]( [name] [varchar](50) ,--学生名字 [subject] [varchar](50) ,--课程名字 [score] [int] ,--分数 [stuid] [int] ,--学号 [d_date] datetime,--登记时间)

2、添加多条数据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 联合使用

2、计算每个人的总成绩并排名(要求显示字段: 学号,姓名,总成绩)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 t1.name,t1.stuid,t2.allscore,t1.d_date from stuscore t1,( select stuid,sum(score) as allscore from stuscore group by stuid)t2where t1.stuid=t2.stuidorder by t2.allscore desc

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

4、计算每个人的平均成绩(要求显示字段: 学号,姓名,平均成绩)selectdistinctt1.stuid,t1.name,t2.avgscorefromstuscoret1,(selectstuid,avg(score)asavgscorefromstuscoregroupbystuid)t2wheret1.stuid=t2.stuid

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

6、统计如下:学号 姓名 语文 数学 英语 总分 平均分select stuid as 学号,name as 姓名, sum(case when subject=&#泌驾台佐39;语文' 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

7、列出各门课程成绩最好的两位学生(要求显示字段: 学号,姓名,科目,成绩)select * from stuscore t1 where t1.stuid in (select top 2 stuscore.stuid from stuscore where subject=t1.subject order by score desc)order by t1.subject

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

9、列出数学成绩的排名(要求显示字段:学号,姓名,成绩,排名)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 联合使用

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 t2.score) t3 order by t3.score desc

11、求出李四的数学成绩的排名declare@tmptable(pmint,namevarchar(50),scoreint,stuidint)insertinto@tmpselectnull,name,score,stuidfromstuscorewheresubject='数学'orderbyscoredescdeclare@idintset@id=0;update@tmpset@id=@id+1,pm=@idselect*from@tmpwherename='李四'

12、统计如下:课程 不及格(0-59)个 良(60-80)个 优(81-100)个selectsubject,(selectcount(*)fromstuscorewherescore<60andsubject=t1.subject)as不及格,(selectcount(*)fromstuscorewherescorebetween60and80andsubject=t1.subject)as良,(selectcount(*)fromstuscorewherescore>80andsubject=t1.subject)as优fromstuscoret1groupbysubject

  • C语言中有 ^ 这个运算符吗
  • 怎么修改命令提示符界面透明度和文字颜色
  • 如何用Verilog语言写一个正弦波函数
  • 手把手教你绘制带箭头的“箭靶图”
  • 如何使用python语言中的方法获取字典中的值
  • 热门搜索
    桂林乐满地旅游攻略 深圳到厦门旅游攻略 奉化旅游景点大全 乌拉盖草原旅游攻略 大渝网旅游 2月份去哪里旅游好 去哪里旅游便宜又好玩 天津旅游教育网 呼伦贝尔旅游局官网 山东青岛旅游攻略