Skip to content
Advertisement

Get top n counted rows in table within group [MySQL]

I’m trying to get just top 3 selling products grouped within categories (just top 3 products by occurrence in transactions (id) count(id) by each category). I was searching a lot for possible solution but with no result. It looks like it is a bit tricky in MySQL since one can’t simply use top() function and so on. Sample data structure bellow:

Advertisement

Answer

In earlier versions of MySQL, I would recommend using variables:

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