Jump to content

[SOLVED] Query about a query


haku

Recommended Posts

Sample data:

 

id1 id2 somecolumn
1    2        text
1    3        text
2    4        text
2    5        text
2    6        text

 

I want to subtract one row for each id1, and the row I want to select is for the highest value of id2. So for the above table, I want rows {1, 3, text} and {2, 6, text}.

 

id1 is non-unique, and will occur many times, but id2 will never occur more than once, even in the id1 table.

 

Can someone give me a hand with this? I've been pondering it for a while now, and haven't been able to come up with anything.

Link to comment
Share on other sites

Something like this:

 

mysql> select id1, max(id2), somecolumn from testing group by id1;
+------+----------+------------+
| id1  | max(id2) | somecolumn |
+------+----------+------------+
|    1 |        3 | text       | 
|    2 |        6 | text       | 
+------+----------+------------+
2 rows in set (0.00 sec)

 

Although I'm not sure what you mean by:

 

I want to subtract one row for each id1
Link to comment
Share on other sites

Maq - I meant that I wanted to select one row for each distinct value in id1, and the row needed to be the row with the highest value of id2. The link PFMaBiSmAd  gave me does exactly that.

 

Thanks PFMaBiSmAd  - that's what I was looking for.

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.