Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 为什么我不能对一个查询进行两次计数_Sql_Count_Impala - Fatal编程技术网

Sql 为什么我不能对一个查询进行两次计数

Sql 为什么我不能对一个查询进行两次计数,sql,count,impala,Sql,Count,Impala,我尝试对查询中的不同列进行两次计数: select count(distinct color) as cid, count(distinct entity) as eid from my_table 上述查询无法处理以下错误: SQLException: [Simba][ImpalaJDBCDriver](500051) ERROR processing query/statement. Error Code: 0, SQL state: TStatus(statusCode:ERRO

我尝试对查询中的不同列进行两次计数:

select  count(distinct color) as cid,
  count(distinct entity) as eid from my_table 
上述查询无法处理以下错误:

SQLException: [Simba][ImpalaJDBCDriver](500051) ERROR processing query/statement. Error Code: 0, SQL state: TStatus(statusCode:ERROR_STATUS, sqlState:HY000, errorMessage:AnalysisException: 
all DISTINCT aggregate functions need to have the same set of parameters as count(DISTINCT color); deviating function: count(DISTINCT entity)
), Query: select  count(distinct color) as cid,
  count(distinct entity) as eid from my_table
但是,如果我只做一次计数,查询就会工作。为什么呢?我可以在一次查询中进行两次计数吗?
谢谢

Impala当前不支持同一查询中的多个计数不同表达式,请参阅。这是一个要求的特性,但令人惊讶的是很难实现,所以还没有添加


现在,如果您不需要精确的精度,可以通过指定NDVcolumn为列生成不同值的估计值;一个查询可以包含多个NDVcolumn实例。要使Impala自动将COUNTDISTINCT表达式重写到NDV,请启用APPX\u COUNT\u DISTINCT查询选项。请参阅。

我不确定这在Impala中是否会起作用,但您可以使用窗口函数和条件聚合来执行COUNTDISTINCT。因此,这个查询:

select count(distinct color) as cid,
       count(distinct entity) as eid
from my_table ;
相当于:

select sum(case when seqnum_color = 1 then 1 else 0 end) as cid,
       sum(case when seqnum_entity = 1 then 1 else 0 end) as eid
from (select t.*, 
             row_number() over (partition by color order by color) as seqnum_color,
             row_number() over (partition by entity order by entity) as seqnum_entity
      from my_table t
     ) t;

这方面的更新—2018年11月发布的Impala 3.1在新的查询块中增加了对多个不同聚合函数的支持

不工作意味着什么?你有错误吗?还是错误的结果?或其他内容?已更新错误消息是否已尝试countdistinct color over,countdistinct entity over?刚刚尝试,以下是错误:错误代码:0,SQL状态:TStatusstatusCode:error_STATUS,sqlState:HY000,errorMessage:AnalysisException:DISTINCT在分析函数中不允许使用:countDISTINCT颜色好主意:D。不幸的是,我的黑斑羚这样会耗尽内存。对于黑斑羚,建议使用NDVentity而不是countDISTINCT实体。@Decula。OP使用countdistinct,因此可能需要精确的distinct计数。NDV很好,但它是近似值。