不能使用 having in impala

cannot use having in impala

我在 impala sql 中使用 "having" 的地方,出现错误 "Could not resolve column/field reference"

select count(dst_ip) as times, dst_ip 
from test_mode 
group by dst_ip 
having times > 1

我不知道为什么,impala 文档支持 https://impala.apache.org/docs/build/html/topics/impala_group_by.html#group_by

很少有数据库支持在 having 子句中重用 select 中定义的别名(想到 MySQL)。我认为这不属于标准 ANSI SQL .

在大多数其他数据库中,例如Impala,您需要重复表达式:

select count(dst_ip) as times, dst_ip 
from test_mode 
group by dst_ip 
having count(dst_ip) > 1

检查您的 impala 版本,移动到上层目录并找到正确的文档。 https://impala.apache.org/docs/build/

例如,Apache Impala Guide(impala-3.4.pdf) 说,在 Overview of Impala Aliases:

From Impala 3.0, the alias substitution logic in the GROUP BY, HAVING, and ORDER BY clauses has become more consistent with standard SQL behavior, as follows. Aliases are now only legal at the top level, and not in subexpressions. The following statements are allowed:

SELECT int_col / 2 AS x  FROM t  GROUP BY x;
SELECT int_col / 2 AS x  FROM t  ORDER BY x;
SELECT NOT bool_col AS nb  FROM t  GROUP BY nb  HAVING nb;

And the following statements are NOT allowed:

SELECT int_col / 2 AS x  FROM t  GROUP BY x / 2;
SELECT int_col / 2 AS x  FROM t  ORDER BY -x;
SELECT int_col / 2 AS x  FROM t  GROUP BY x HAVING x > 3;

在您的 sql 中,times > 1 是一个子表达式,having times > 1 在 Impala 3.0 或更高版本中是不可接受的。