Hive [错误10025]表达式不在GROUP BY中

时间:2017-03-08 17:34:22

标签: hadoop hive

第一个查询效果很好。但第二个查询给出错误“[错误10025]表达式不在GROUP BY键'-3''”。我阅读了相关的帖子,到目前为止我不知道如何修复错误。

非常感谢你对此的帮助。

查询#1

SELECT case when user_id = '-3' then cast(order_id as varchar(255)) else user_id end as user_id2
from orders
where user_id = '-3' 

查询#2

SELECT case when user_id = '-3' then cast(order_id as varchar(255)) else user_id end as user_id2
       ,sum(order_value) as sales
from orders
group by 1


Traceback (most recent call last):
File "/home/net1/liulu001/conversant/troubleshooting_userid.py", line 28, in <module>
cursor.execute(q)
File "/home/net1/liulu001/.local/lib/python2.6/site-packages/impala/hiveserver2.py", line 302, in execute
configuration=configuration)
File "/home/net1/liulu001/.local/lib/python2.6/site-packages/impala/hiveserver2.py", line 343, in execute_async
self._execute_async(op)
File "/home/net1/liulu001/.local/lib/python2.6/site-packages/impala/hiveserver2.py", line 362, in _execute_async
operation_fn()
File "/home/net1/liulu001/.local/lib/python2.6/site-packages/impala/hiveserver2.py", line 340, in op
async=True)
File "/home/net1/liulu001/.local/lib/python2.6/site-packages/impala/hiveserver2.py", line 1027, in execute
return self._operation('ExecuteStatement', req)
File "/home/net1/liulu001/.local/lib/python2.6/site-packages/impala/hiveserver2.py", line 957, in _operation
resp = self._rpc(kind, request)
File "/home/net1/liulu001/.local/lib/python2.6/site-packages/impala/hiveserver2.py", line 925, in _rpc
err_if_rpc_not_ok(response)
File "/home/net1/liulu001/.local/lib/python2.6/site-packages/impala/hiveserver2.py", line 704, in err_if_rpc_not_ok
raise HiveServer2Error(resp.status.errorMessage)
impala.error.HiveServer2Error: Error while compiling statement: FAILED:       SemanticException [Error 10025]: Line 3:13 Expression not in GROUP BY key ''-3''

1 个答案:

答案 0 :(得分:0)

您没有在第一个查询中进行聚合,因此不需要group by。 在第二个查询中,group by 1被视为常量。请改用group by case when user_id = '-3' then cast(order_id as varchar(255)) else user_id end

或者,如果您希望能够按位置而不是按名称引用列名,请在查询之前尝试:

set hive.groupby.orderby.position.alias=true;