Python全系列 教程
3567个小节阅读:5930.4k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
group by通常和聚合函数一起使用,按照一个或者多个列进行分组,然后在对每组数据进行聚合操作。
案例实战:
查询每个部门的平均工资、最高工资、最低工资
xxxxxxxxxx
hive> select deptno,avg(sal) avg_sal,max(sal) max_sal,min(sal) min_sal from emp group by deptno;
Query ID = root_20211115150801_985c1ac7-9260-423b-a9b0-72eae86b3b92
Total jobs = 1
Launching Job 1 out of 1
Number of reduce tasks not specified. Estimated from input data size: 1
In order to change the average load for a reducer (in bytes):
set hive.exec.reducers.bytes.per.reducer=<number>
In order to limit the maximum number of reducers:
set hive.exec.reducers.max=<number>
In order to set a constant number of reducers:
set mapreduce.job.reduces=<number>
Starting Job = job_1636950452604_0003, Tracking URL = http://node4:8088/proxy/application_1636950452604_0003/
Kill Command = /opt/hadoop-3.1.3/bin/mapred job -kill job_1636950452604_0003
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 1
10 2916.6666666666665 5000.0 1300.0
20 2175.0 3000.0 800.0
30 1566.6666666666667 2850.0 950.0
使用group by语句后,select语句中执行出现group by语句中的列名和聚合函数。
xxxxxxxxxx
hive> select deptno,avg(sal) avg_sal,max(sal) max_sal,min(sal) min_sal,ename from emp group by deptno;
FAILED: SemanticException [Error 10025]: Line 1:65 Expression not in GROUP BY key 'ename'
where语句中不能出现聚合函数,或者通过聚合函数计算的结果。
having语句只能用在group by语句的后面,在一个没有group by语句的sql中不能出现having语句。
案例实战:
查询每个部门平均薪资大于1800的部门的平均工资、最高工资、最低工资。
xxxxxxxxxx
hive> select deptno,avg(sal) avg_sal,max(sal) max_sal,min(sal) min_sal from emp group by deptno where avg_sal>1800;
FAILED: ParseException line 1:90 missing EOF at 'where' near 'deptno'
hive> select deptno,avg(sal) avg_sal,max(sal) max_sal,min(sal) min_sal from emp group by deptno having avg_sal>1800;
Query ID = root_20211115151258_0b1728f2-27f6-4b33-84aa-15a93421212b
Total jobs = 1
Launching Job 1 out of 1