如何在蜂巢中实现百分位数

时间:2019-07-07 08:09:07

标签: hadoop hive percentile

我在蜂巢中有一个这样的桌子

user_id     no.of game_plays
u1           52
u2           190
u10          166
u9           100
u3           90
u4           44
u5           21
u7           10
u8           5

以上只是非常小的数据样本。

因此,游戏总播放次数为 678

我想像下面那样计算每个组中的用户

who contribute to top 33.3% of total game_plays and 
who contribute to between 33.3% and 66.6% of total game_plays 
who contribute to bottom 33.3% of total game_plays

基本上,将数据按上述方式分成3组,并从每组中获取前20名用户。

我知道如何在BigQuery中实现的逻辑,例如...获取game_plays排序的百分比值,然后在上述查询中放入case语句,并在每个组中使用game_plays进行排名,然后选择等级<= 20

给出我想要的结果。

我不知道如何在蜂巢中实现这种事情。

我浏览了以下页面,但没有任何想法

How to implement percentile in Hive?

How to calculate median in Hive

并通过下面的功能链接

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Types

我知道我必须使用百分位功能...但是不知道我该怎么实现。

下面是我尝试过的代码,

select a.user_id,a.game_plays, percentile(a.game_plays,0.66) as percentile
from (
select user_id, sum(game_plays) as game_plays
from game_play_table
where data_date = '2019-06-01' 
group by user_id) a

我知道上面的代码没有给出确切的give输出,但是在它上面写了一个外部查询之后。...我可以得到我想要的输出....但是上面的查询输出本身是非常不同的。 / p>

任何人都可以帮忙吗???

1 个答案:

答案 0 :(得分:0)

  

您可以使用“案例” 计算百分位数

select user_id,game_plays ,
case when (game_plays * (100 /678)) > 33.3 then 'top 33.3%'
when (game_plays * (100 /678)) > 33.3) and (game_plays * (100 /678)) < 66.6) then 'between 33.3% and 66.6%'
when (game_plays * (100 /678)) < 33.3) then 'less then 33.3%'
end as percentile 
from game_play_table
where data_date = '2019-06-01' 
group by user_id