Spark sql top n per group

前端 未结 1 977
一向
一向 2020-12-01 13:22

How can I get the top-n (lets say top 10 or top 3) per group in spark-sql?

http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-

相关标签:
1条回答
  • 2020-12-01 13:55

    You can use the window function feature that was added in Spark 1.4 Suppose that we have a productRevenue table as shown below.

    the answer to What are the best-selling and the second best-selling products in every category is as follows

    SELECT product,category,revenue FROM 
       (SELECT product,category,revenue,dense_rank() 
             OVER (PARTITION BY category ORDER BY revenue DESC) as rank 
        FROM productRevenue) tmp 
    WHERE rank <= 2
    

    Tis will give you the desired result

    0 讨论(0)
提交回复
热议问题