Hive中提示Expression Not In Group By Key的解决办法

问题出现的原因:

1.Hive不允许直接访问非group by字段;
2.对于非group by字段,可以用Hive的collect_set函数收集这些字段,返回一个数组;
3.使用数字下标,可以直接访问数组中的元素;

MySQL中代码:

select d.dname,count(*) num from emp e 
join dept d on e.deptno=d.deptno 
where e.sal<=2500 
group by e.deptno 
order by num desc limit 1

HiveQL中代码:

select collect_set(d.dname)[0],count(*) num from emp e 
join dept d on e.deptno=d.deptno 
where e.sal<=2500 
group by e.deptno 
order by num desc limit 1

使用collect_set()函数包围非group by字段后,问题解决。

你可能感兴趣的:(Hive中提示Expression Not In Group By Key的解决办法)