首页 > 解决方案 > 如何将查询答案传递给限制函数 Impala

问题描述

我正在尝试在 impala 中抽取 20% 的表格。我在某处听说内置的 impala 采样功能存在问题。

有没有办法将子查询传递给 impala 限制函数以对整个表的 n% 进行采样。

我有这样的事情:

select 
* from
table_a
order by rand()
limit
(
select 
round( (count(distinct ids)) *.2,0)
from table_a) 
)

子查询给了我所有记录的 20%

标签: sqlhadoopimpala

解决方案


我不确定 Impala 是否有特定的采样逻辑(某些数据库有)。但是您可以使用窗口函数:

select a.*
from (select a.*,
             row_number() over (order by rand()) as seqnum,
             count(*) over () as cnt
      from table_a
     ) a
where seqnum <= cnt * 0.2;

推荐阅读