在类别内进行排序并将此配置单元表限制为5 [英] sort within category and limit 5 for this hive table

查看:131
本文介绍了在类别内进行排序并将此配置单元表限制为5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个配置表格A,它有以下列:

I have a hive table A that has the following column

USER   ITEM    SCORE
U1      I1       S1
U1      I2       S2
...................

我想要的是一个表格B这样的格式

What I want is a table B such a format

USER    ITEMS    #ITEMS is an array
 U1     [I2,I3,...]   # items are sorted according to score in descending and limit 5

对于用户而言,只有少于5个项目,只需将项目按降序排列即可。

for users have less than 5 items, just put the array with items in descending order.

推荐答案

这:

should be something like this :

select USER,collect_set(ITEM) from (
    select USER, ITEM,row_number () over (partition by USER order by SCORE desc) RN 
    from A
) t1
where RN <= 5
group by USER;

这篇关于在类别内进行排序并将此配置单元表限制为5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆