Skip to content
Advertisement

How to get the first row per group?

I have a query like this:

The result looks like this:

Now I want to get the first row for each category_id. So it must be the biggest num and its business_id. So the expected result would be:

How can I do that?

Advertisement

Answer

if your MySQL version support ROW_NUMBER + window function, you can try to use ROW_NUMBER to get the biggest num by category_id

Query #1

num business_id category_id
22 5543 8
13 3242 11

View on DB Fiddle

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