关于在hive中使用 group by 分组的误区
在hive中使用group by查询,如:
select name,sex from people group by sex;
会出现报错提示:
FAILED: SemanticException [Error 10025]: Line 1:15 Expression not in GROUP BY key 'name'
Error: Error while compiling statement: FAILED: SemanticException [Error 10025]: Line 1:15 Expression not in GROUP BY key 'name' (state=42000,code=10025)
问题提取:
Expression not in GROUP BY key 'xxx'
原因分析:
hive中不允许直接访问非 group by 的字段
解决方法:
1-- 查询字段仅限于group by 的字段,后面也可以查询count等聚合函数,如:
select sex from people group by sex;
查询显示如下:
| sex |
| 女 |
| 男 |
select sex,count(name) from people group by sex;
查询显示如下:
| sex |_c1