oracle数据库之分组查询(转)

  本章内容和大家分享的是数据当中的分组查询。分组查询复杂一点的是建立在多张表的查询的基础之上,(我们在上一节课的学习中已经给大家分享了多表查询的使用技巧,大家可以自行访问:多表查询1  多表查询2)而在实际项目中的分组查询的使用也是比较多的,作为初学者来说,学好分组查询的姿势是十分有必要的!

分组查询概念

什么是分组查询:分组查询是按照一定的规则进行分组,分组以后数据会聚合,需要使用聚合函数,但是使用聚合函数不一定要分组,分组的关键字是group by。

首先,我们先来思考一下下面的sql语句的正确性:

---1.select * from t_student group by sex;     
---按照性别分组 男或者女 但是实际显示的是所有性别,*是所有 所以会报错,因为这种方式不是group by 表达式

查看结果:

---2.select sex from t_student;               
---这种方式最后显示的是所有的男女,也不是分组我们想要的 男 女  数据 

查看结果:

---3.select t.studentid,t.sex from t_student t group by sex;  --6条id数据对应2条数据 不是group by表达式

查询结果:

---4.select sex from t_student group by sex;
---按照性别分组,只显示性别没有任何毛病--成功

查看结果:

我们看一下聚合函数的使用,聚合函数有count()  sum()  max()  min()  avg()  round() 

复制代码
--聚合函数
count() ---统计
sum()   ---求和
max()   ---最大值
min()   ---最小值
avg()   ---平均值
round() ---四舍五入函数
复制代码

 下面小编给大家演示一下关于这些聚合函数的基础操作,有助于我们后面的分组使用:

复制代码
select max(age) from t_student;
--求年龄的最大值

select count(studentid) from t_student;
--求学生总数

select sum(studentid) from t_student;
--所有学生id的和

select min(studentid) from t_student; 
--求学生id中的最小值

select avg(studentid) from t_student;
--求学生id的平均值
复制代码

再议聚合函数:

select sex,sum(age) from t_student group by sex;
--以性别分组,显示性别中的男女年龄的之和

查看结果:

select sex,count(studentid) from t_student group by sex;
--以学生性别分组,显示男生和女生分别有多少?

 查看结果:

select sex,max(age) from t_student group by sex;
--以性别分组,显示男生中年龄最大的和女生中年龄最大的。

 查看结果:

ok,我们通过上面的例子来继续练习一下分组查询。 

 --统计706班男生,女生人数?

select sex,count(t2.studentid) from t_class t1,t_student t2 where t1.classid=t2.classid and t1.classid=1
group by sex;

查询结果:

--查询826班男生、女生年龄的总和

select sex,sum(t2.age) from t_class t1,t_student t2 where t1.classid=t1.classid 
and t1.classid=3 group by sex;

 查看结果:

--查询每门课程的总分、最高分、最低分、平均分

--查询每门课程的总分、最高分、最低分、平均分
select t1.coursename 课程名字,max(t2.score)最高分,min(t2.score)最低分,avg(t2.score)平均分 from t_course t1,t_score t2 where t1.courseid=t2.courseid
group by t1.coursename;

查看结果:

--查询826班每位学生每门课程的总分、平均分、最高分、最低分

复制代码
select t3.coursename 课程名字,t2.name 学生姓名,
       sum(t4.score) 总分,
       avg(t4.score) 平均分,
       max(t4.score) 最高分,
       min(t4.score) 最低分
  from t_class t1, t_student t2, t_course t3, t_score t4
 where t1.classid = t2.classid
   and t2.studentid = t4.studentid
   and t4.courseid = t3.courseid
   and t1.classid = 3
 group by t3.coursename,t2.name;
复制代码

查看结果:

--查询每个学员的总分、平均分、最高分、最低分,只显示总分在250分以上的学员

敲笔记了哈:

1.where 使用在分组查询之前,where后面不能使用聚合函数

2.having 分组之后的过滤,having 紧跟group by ,有having 一定有group by ,但是有group by 不一定有having  

3.having 后面可以使用聚合函数 

复制代码
--显示姓名
select t1.name 学生姓名, sum(t2.score) 总分, avg(t2.score) 平均分, max(t2.score) 最高分, min(t2.score) 最低分 from t_student t1, t_score t2 where t1.studentid = t2.studentid group by t1.name having sum(t2.score)>250;
复制代码

查看结果:

另外的一种展示结果:

复制代码
--不显示性名
select sum(t2.score) 总分,
       avg(t2.score) 平均分,
       max(t2.score) 最高分,
       min(t2.score) 最低分
  from t_student t1, t_score t2
 where t1.studentid = t2.studentid
 group by t1.studentid
having sum(t2.score) > 250;
复制代码

查询结果:

到此,分组查询我们就介绍到这里了。。。

作者:丨Fighter.Lu丨 点滴记录,开源共享。帮助更多有需要的人解决问题!博主博客地址:http://www.cnblogs.com/fighter007/
原文地址:https://www.cnblogs.com/qdck/p/10843130.html