Hive 查询,仅选择特定百分位数的记录



我有两列的表格 - ID 和总持续时间:

id  tot_dur
123 1
124 2
125 5
126 8

我想有一个仅选择第 75 个百分位数的 Hive 查询。它应该只是最后一条记录:

id  tot_dur
126 8

这就是我所拥有的,但我很难理解 OVER(( 和 PARTITIONED BY(( 函数的使用,因为根据我的研究,这是我应该使用的函数。在我获得tot_dur列之前,我应该sumgroup byduration。不确定percentile是否是正确的功能,因为我发现了percentile_approx的用例。

select k1.id as id, percentile(cast(tot_dur as bigint),0.75) OVER () as tot_dur
from (
SELECT id, sum(duration) as tot_dur 
FROM data_source
GROUP BY id) k1
group by id

如果我猜对了,这就是你想要的:

with data as (select stack(4,
123, 1,
124, 2,
125, 5,
126, 8) as (id,  tot_dur))
-----------------------------------------------------------------------------
select data.id, data.tot_dur 
from data 
join (select percentile(tot_dur, 0.75) as threshold from data) as t 
where data.tot_dur >= t.threshold;

最新更新