目录
- hive outline
- hive SQL查询执行顺序
- hive 聚合函数处理null的注意事项
- hive count
- hive count(*)&count(1)&count(字段名称)的区别
- hive distinct
- hive where
- hive where的使用条件
- hive left join 时候 where 和 on 区别
- hive group by
- hive group by的使用条件
- hive group by 和 partition by的区别
- hive having
- 关键字having作用
- where 和 having 区别
- hive Union&UNION ALL 联合查询
- hive from子句中子查询
- hive where子句中子查询
- hive CTE
- from风格(查询)
- chaining CTEs 链式(查询)
- insert+ cte 创建表
- create as+ cte 创建表
hive outline
链接
hive SQL查询执行顺序
from>on>join>where>group by>聚合函数(例如分组后统计数量)>having>select>distinct>order>limit
聚合语句
(sum,min,max,avg,count)
hive 聚合函数处理null的注意事项
假设有以下一张表:test_null
c1 | c2 |
aaa | 3 |
bbb | 3 |
ccc | null |
ddd | 6 |
总结:
- count(*) 包含 null 值,统计所有行数
- count(id) 不包含 null 所在行
- min&max&avg&sum 运算时不包括null所在行
hive count
hive count(*)&count(1)&count(字段名称)的区别
简单来说:
count(*)==count(1)
count(1):
统计所有行,包含null
count(字段名称):
统计所有行,但不包含该字段下为null的行
hive distinct
- 返回所有匹配的行 去除重复的行
- 整体去重
选美:筛选出每个部门身高不重复的美女
hive where
hive where的使用条件
- where 条件中不能使用聚合函数
原因:使用聚合函数的前提是结果集已经确定,where子句还处于在“确定”结果集的过程中 - 聚合函数有 count sum avg 等
hive left join 时候 where 和 on 区别
- on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左表记录
- where条件是在临时表生成好后,再对临时表进行过滤的条件
hive group by
hive group by的使用条件
select 后字段只能是 group by 分组的字段,或者是被聚合函数应用的字段
hive group by 和 partition by的区别
group by 汇总后行数减少,partition by汇总后原表中的行数没变
hive having
关键字having作用
如果要对分组后的各组数据要进一步筛选,那么可以使用关键子having
where 和 having 区别
- having 是在 group by 分完组之后对数据再次进行筛选的,所以 having 要筛选的字段只能是分组字段或者聚合函数
- where 是直接对数据表中的字段直接进行的筛选的
hive Union&UNION ALL 联合查询
UNION
UNION ALL
如果要将order by,sort by,cluster by,distribute by或limt应用于单个SELECT,请将子查询放在括号内
如果要将order by,sort by,cluster by,distribute by或limt应用于整个UNION结果,请将其放在最后
hive from子句中子查询
在Hive0.12版本,仅在FROM子句中支持子查询,而且
必须要给子查询一个名称,因为FROM子句中的每个表都必须有一个名称
hive where子句中子查询
- 在Hive0.13版本后就支持了where子句中子查询,前提是
in、not in
这样的子查询
上述查询又称之为:不相关子查询
- 同样支持 EXISTS 和 NOT EXISTS 类型的子查询
上述查询又称之为:相关子查询
hive CTE
以下的图片中的文字说明,用词可能不准确,但我这里想表达的是,梳理语句脉络
from风格(查询)
chaining CTEs 链式(查询)
insert+ cte 创建表
create as+ cte 创建表