Select the first row of each group in Mysql

Asked

Viewed 784 times

4

Suppose I have such a table:

item   | group
-------+--------
item a | group x
item b | group y
item c | group y
item d | group x
item e | group z

And that I want to select a single item to represent each group.

Now, you might be thinking, "Okay, but which item? Because each group has several!"

So, the rule is as follows: I simply want to sort my table according to some column, which in this specific case will be the item column, and then associate the first row of each group with the group itself, through the GROUP BY command (or other more suitable).

Then, after the ordination, it would look like this:

ORDER BY item DESC

item   | group
-------+--------
item e | group z
item d | group x
item c | group y
item b | group y
item a | group x

And grouping according to the group column, it should look like this:

GROUP BY group

item   | group
-------+--------
item e | group z
item d | group x
item c | group y

But it won’t be! Whenever I try to do these commands, Mysql ignores the sorting command and groups as if the table had not been reoorded before, thus being grouped together:

item   | group
-------+--------
item a | group x
item b | group y
item e | group z

PS1: I saw some solutions to this problem through the OVER and PARTITION BY command. But they don’t work in Mysql.

PS2: Note that my table does not have Numbers, only strings. With numbers could be easier, but the intention is to generalize the solution to string even.

Thanks in advance!

  • You can improve the question, and post your querie. Maybe you can help!

  • 3

    Maybe I’m oversimplifying the problem, but it seems to me that you just want the "biggest" item in each group. Something like SELECT MAX(item), grupo FROM tabela GROUP by grupo doesn’t solve your problem?

  • Rafael, sorry if I couldn’t be clear, but Anthony killed the riddle! That’s right! The MAX function solves the question!

1 answer

6


I created a test fiddle and the reply given in comment by @Anthony Accioly meets what you need:

SELECT MAX(item), grupo 
FROM tabela 
GROUP BY grupo

edited:

Considering the comments, I believe that this answer.
Detail: the solution will only be correct if the field item_1 is unique.

SELECT item_1, item_2, grupo 
FROM tabela t1 
WHERE t1.item_1 = (select max(t2.item_1) from tabela t2 where t2.grupo = t1.grupo); 
  • But what the hell!!! I spent Sunday trying harder things and this simple MAX function was the one that worked! Fight huh! Mew!!! Thanks a lot!!! But, let me ask: What if I wanted the second largest? Or the third? How could I do it?

  • No, wait, it didn’t work!!! MAX is taking the largest of the item column, but what if my example had other item columns as well (like: column_item_1, column_item_2, column_item_3)? Not necessarily the MAX value of the column_item_1 will match the same row of the column_item_2 and 3.

  • Here, see: http://rextester.com/BRQL1014

  • @Can Jhenry give an example? because I understand that if there is another column that controls who should be the biggest, just put it as a control column (ex MAX(coluna_x) )

  • I missed the link. This is right: http://rextester.com/TPJVRR39858 Note that in the query result, the row in the item_1 column does not match the same row in the item_2 column. This is wrong. I wanted the lines if they corresponded, were the same. I wanted the result to look like this: item_1|item_2|group ------+-----+------- item d|item j|group x item c|item i|group y item e|item m|group z

  • 1

    @Jhenry see if you’ve solved ;]

  • That’s it, that’s it, that’s it!!! I didn’t quite understand how you used the Where but the fact is that it worked!!! Could you give me more explanation about the reasoning of the last line?

  • 1

    @Jhenry || where t2.grupo = t1.grupo -> will catch everyone who is in the same group (ex group x) || max(t2.item_1) -> gets the biggest item_1. Until then we have the same thing from the first consultation (select MAX(it...)) || WHERE t1.item_1 = (SE... -> this consults the data of that item_1 specific, avoiding to bring random data.

  • Very good!!! Are you freelance? Get projects over the internet too!? As need some dev to help me, we could negotiate for 99freela...

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.