Jump to content

Selecting the group-wise maximum


Hi I Am Timbo

Recommended Posts

I'm trying to write a query on a user table.  The table looks like this:

 

+--------+-----------+-----------+----------+----------------+
| idUser | username  | firstname | lastname | originalIdUser |
+--------+-----------+-----------+----------+----------------+
|      1 | tbarsness | Tim       | Barsness |              1 |
|      2 | asdf      | Tim       | Barsness |              1 |
|      3 | tbarsness | Tim       | Barsness |              1 |
|      4 | rocks     | Tim       | Barsness |              1 |
|      5 | johndoe   | John      | Doe      |              5 |
|      6 | agoobwa   | John      | agoobwa  |              5 |
|      7 | agoobwa   | John      | agoobwa  |              5 |
+--------+-----------+-----------+----------+----------------+

 

I am trying to get the whole row for group of originalIdUser having a max(idUser)

 

The query I thought should work:

 

SELECT * , max( idUser )
FROM `user`
GROUP BY originalIdUser
HAVING idUser = max( idUser ) 

 

But it returns an empty result set.  This is because this query:

 

SELECT * , max( idUser )
FROM `user`
GROUP BY originalIdUser

 

returns the first record in each group as below:

 

+--------+-----------+-----------+----------+----------------+
| idUser | username  | firstname | lastname | originalIdUser |
+--------+-----------+-----------+----------+----------------+
|      1 | tbarsness | Tim       | Barsness |              1 |
|      5 | johndoe   | John      | Doe      |              5 |
+--------+-----------+-----------+----------+----------------+

 

This is what I wanted:

 

+--------+-----------+-----------+----------+----------------+
| idUser | username  | firstname | lastname | originalIdUser |
+--------+-----------+-----------+----------+----------------+
|      4 | rocks     | Tim       | Barsness |              1 |
|      7 | agoobwa   | John      | agoobwa  |              5 |
+--------+-----------+-----------+----------+----------------+

 

I am thinking I have to join the user table to itself, but I am not sure how.  Any help would be greatly appreciated.

 

 

Link to comment
Share on other sites

Thankyou Timbo for a perfectly worded question :)  That's how artacus was able to answer it so easily.

 

It's a relief after reading so many "Please help, my file doesn't send the file into the database" type questions..

Link to comment
Share on other sites

That's how artacus was able to answer it so easily.

 

LOL. That and the summer internship I did with the psychic friends network REALLY comes in handy. Lucky for us, fiber optic cable channels psychic energy even better than copper. Wonder how old Dion is doing, haven't seen her in  a while.

Link to comment
Share on other sites

Thankyou Timbo for a perfectly worded question :)  That's how artacus was able to answer it so easily.

 

It's a relief after reading so many "Please help, my file doesn't send the file into the database" type questions..

But isn't that a correlated subquery (I'd have to run explain to check for sure)?  You should be able to use a derived table and get better performance, IMHO.

Link to comment
Share on other sites

But isn't that a correlated subquery (I'd have to run explain to check for sure)?

It is.

What Fenway is saying is that since you are looking up all your users anyway, do the subquery in the FROM section so you can do two queries instead of n+1 queries in the WHERE clause.

Link to comment
Share on other sites

But isn't that a correlated subquery (I'd have to run explain to check for sure)?

It is.

What Fenway is saying is that since you are looking up all your users anyway, do the subquery in the FROM section so you can do two queries instead of n+1 queries in the WHERE clause.

It's worse than n+1... it's one extra query _for every row_ in the outer query!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.