If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Query Help and finding the highest version number

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-29-10, 16:47
sunfire sunfire is offline
Registered User
 
Join Date: Aug 2003
Posts: 19
Query Help and finding the highest version number

Hello,
I think I've been starring at this too long :-p

what I would like to do is, I would like to return all of the rows with the highest version number for the group_id.

Quick Table
app_id, name, group_id, app_ver
1000, Foo, com.foo, 1.0
1001, Foo, com.foo, 1.1
1020, Foo, com.foo, 1.4
1005, Bar, com.bar, 1.1
1006, Bar, com.bar, 1.2

Expected Results
1020, Foo, com.foo, 1.4
1006, Bar, com.bar, 1.2

Thanks,
tom
Reply With Quote
  #2 (permalink)  
Old 03-29-10, 18:47
rayqsl rayqsl is offline
Registered User
 
Join Date: Jan 2010
Posts: 18
Something like this.

select q.app_id, q.name, q.group_id, q.app_ver
from quick q
join (select group_id, max(app_ver) as max_app_ver
from quick
group by group_id) a on a.group_id = q.group_id
and a.max_app_ver = q.app_ver
Reply With Quote
  #3 (permalink)  
Old 03-30-10, 08:46
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Or:

Code:
select q.app_id, q.name, q.group_id, q.app_ver
     from quick q
where q.app_ver =(select max(app_ver) from quick q2
                          where q2.unique_cols = q.unique_cols)
Dave
Reply With Quote
  #4 (permalink)  
Old 03-30-10, 09:18
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
dave, i've seen multiple times (no links, though, sorry) where someone has done a benchmark of the "join to max derived table" (rayqsl's version) versus the "where max correlated subquery" (your version) and the derived table always outperforms
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 03-30-10, 11:30
sunfire sunfire is offline
Registered User
 
Join Date: Aug 2003
Posts: 19
Thumbs up

Thanks, the join makes sense this help a bunch.

thanks again,
tom
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On