Skip to content
Advertisement

How to get top N rows with some conditions

I have a query something like this:

But now, I want to see the top 10 product_id for each site and category_id based on the clicks. When I write the LIMIT function, it only shows the top 10 products but it does not group it by category_id and shop_id.

How can I do this?

Advertisement

Answer

Use window functions. You can RANK() records by descending clicks within site/category partitions in a subquery, and then filter in the outer query:

I am unclear on why you need the coalesce()/cast() logic in the sum() (just like other aggregate functions, sum() ignore null values, and it seems like #clicks is a number already), so I removed it – you can add it back if you do need it, for some reason that I could not think of.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement