Hive如何在GROUP BY 后,获取每个GROUP的Top N

转载自:http://stackoverflow.com/questions/9390698/hive-getting-top-n-records-in-group-by-query

原标题:Hive getting top n records in group by query

SELECT page-id, user-id, clicks
FROM (
    SELECT page-id, user-id, rank(page-id) as rank, clicks FROM (
        SELECT page-id, user-id, clicks FROM mytable
        DISTRIBUTE BY page-id
        SORT BY page-id, clicks desc
) a ) b
WHERE rank < 5
ORDER BY page-id, rank

One thought on “Hive如何在GROUP BY 后,获取每个GROUP的Top N

Leave a Reply

Your email address will not be published. Required fields are marked *