6.2.8 聚合函数
1) 语法
-
count(*),表示统计所有行数,包含null值;
-
count(某列),表示该列一共有多少行,不包含null值;
-
max(),求最大值,不包含null,除非所有值都是null;
-
min(),求最小值,不包含null,除非所有值都是null;
-
sum(),求和,不包含null。
-
avg(),求平均值,不包含null。
2) 案例实操
(1) 求总行数 (count)
sql
hive (default)> select count(*) cnt from emp;
hive sql执行过程:
![](https://img-blog.csdnimg.cn/direct/2549336f521b40aaa05f9f5eebc3273a.png)
![](https://img-blog.csdnimg.cn/direct/e3d67a26b26946689ebf1bd186a471e2.png)
count不仅可以传一个 *
还可以传一个(字段)
![](https://img-blog.csdnimg.cn/direct/13315666554f46fa91f5e438e1d1133f.png)
null 值是不统计在内
![](https://img-blog.csdnimg.cn/direct/6e44fef83e1141f4b6d73e847b41c7f9.png)
null 值是不会被统计在内的
(2) 求工资的最大值 (max)
sql
hive (default)> select max(sal) max_sal from emp;
hive sql执行过程:
![](https://img-blog.csdnimg.cn/direct/d76f5b9275b54ecaa058a842f6a8245e.png)
![](https://img-blog.csdnimg.cn/direct/a47609f63ba64c59854469de43dbd060.png)
(3) 求工资的最小值 (min)
sql
hive (default)> select min(sal) min_sal from emp;
hive sql执行过程:
![](https://img-blog.csdnimg.cn/direct/a15e5610ca5d4470b94df51785666e7c.png)
![](https://img-blog.csdnimg.cn/direct/9475f256d2f94380ad3ca9f289941d7f.png)
(4) 求工资的总和 (sum)
sql
hive (default)> select sum(sal) sum_sal from emp;
hive sql执行过程:
![](https://img-blog.csdnimg.cn/direct/862ad9d528f845959f2e9c92282d2658.png)
![](https://img-blog.csdnimg.cn/direct/e2201ef6741e4baeb9c63715bd2c2c6a.png)
(5) 求工资的平均值 (avg)
sql
hive (default)> select avg(sal) avg_sal from emp;
hive sql执行过程:
![](https://img-blog.csdnimg.cn/direct/281aab626a41400bab21aef00901250d.png)
![](https://img-blog.csdnimg.cn/direct/b4b7558bbdf4463ebd64302b56d51201.png)
6.3 分组
6.3.1 Group By 语句
Group By语句通常会和聚合函数一起使用,按照一个或者多个列队结果进行分组,然后对每个组执行聚合操作。
1) 案例实操:
把job分组后查看
sql
select count(*) from emp group by job
![](https://img-blog.csdnimg.cn/direct/27d37a160b604de892872ffb150f6642.png)
结果:
![](https://img-blog.csdnimg.cn/direct/7366d5bacc59449083292612a047bd30.png)
看起来有点不太对劲,再改一下:
在count(*)前面加个job
![](https://img-blog.csdnimg.cn/direct/2c5e4079116c49d09c9bf08789ebaba1.png)
现在看的清楚多了
注意:
![](https://img-blog.csdnimg.cn/direct/8aa2531df3514f909a204b07b0f44958.png)
注意: 当你 group by job 的时候,
那你只能select 后面跟 job 不能是其他的字段
(1) 计算emp表每个部门的平均工资
sql
hive (default)>
select
t.deptno,
avg(t.sal) avg_sal
from emp t
group by t.deptno;
hive sql执行过程:
![](https://img-blog.csdnimg.cn/direct/377ddde5d3854c8287fbfbba9a7241e7.png)
(2)计算emp每个部门中每个岗位的最高薪水。
sql
hive (default)>
select
t.deptno,
t.job,
max(t.sal) max_sal
from emp t
group by t.deptno, t.job;
hive sql执行过程:
![](https://img-blog.csdnimg.cn/direct/af4d014c072542989104eb8c11068e13.png)
6.3.2 Having 语句
1) having 与 where 不同点
(1) where 后面不能写分组聚合函数,而having后面可以使用分组聚合函数
(2) having 只用于 group by 分组统计语句
2) 案例实操
找这张表里人数大于等于2的:
![](https://img-blog.csdnimg.cn/direct/f86865454fda49af99cac9fd9cd802de.png)
输入代码:
![](https://img-blog.csdnimg.cn/direct/b9c44808b6884b5b835309a1b650eb93.png)
得到输出结果:
![](https://img-blog.csdnimg.cn/direct/8ad57ebc54d2411e8b359a0f0fd13638.png)
但是当我不想要嵌套子查询时:
![](https://img-blog.csdnimg.cn/direct/60c49d4900f44d638b29f0c50b78e16e.png)
可见where字句行不通
因为where是用来过滤一行一行的数据
而已经group by 分组过了,where不能过滤组
于是我们只能用having来过滤一组一组的数据
那我们就用having:
![](https://img-blog.csdnimg.cn/direct/a383bcb359284df79d83da972b6c0575.png)
(1) 求每个部门的平均薪水大于2000的部门
1.求每个部门的平均工资
sql
hive (default)>
select
deptno,
avg(sal)
from emp
group by deptno;
hive sql执行过程:
![](https://img-blog.csdnimg.cn/direct/b38a2c1d927e4e37b0f1e5fd287e144b.png)
2.求每个部门的平均薪水大于2000的部门
sql
hive (default)>
select
deptno,
avg(sal) avg_sal
from emp
group by deptno
having avg_sal > 2000;
hive sql执行过程:
![](https://img-blog.csdnimg.cn/direct/4b4aea98cfd14a61adf1cb06f42cf5c7.png)