我的Impala查询中出现以下错误:
select
upload_key,
max(my_timestamp) as upload_time,
max(color_key) as max_color_fk,
count(distinct color_key) as color_count,
count(distinct id) as toy_count
from upload_table
group by upload_key
Run Code Online (Sandbox Code Playgroud)
并得到错误:
AnalysisException:所有DISTINCT聚合函数都需要具有与count相同的参数集(DISTINCT color_key); 偏差函数:count(DISTINCT id)
我不知道为什么会出现这个错误.我所做的是为每个小组(按分组upload_key),我试图计算多少distinct id以及多少distinct color_key.
有谁有想法吗
错误消息表明DISTINCT只允许在一个列[组合]上,但您尝试两个,color_key&id.解决方法是两个选择,然后是一个连接:
select
t1.upload_key,
t1.upload_time,
t1.max_color_fk,
t1.color_count,
t2.toy_count
from
(
select
upload_key,
max(my_timestamp) as upload_time,
max(color_key) as max_color_fk,
count(distinct color_key) as color_count
from upload_table
group by upload_key
) as t1
join
(
select
upload_key
count(distinct id) as toy_count
from upload_table
group by upload_key
) as t2
on t1. upload_key = t2.upload_key
Run Code Online (Sandbox Code Playgroud)