MySQL Forums
Forum List  »  General

selecting distinct records with group max value
Posted by: Tom Byars
Date: May 17, 2006 09:40AM

I have a table (Tbl) primary indexed on Fld1, Fld2, Fld3 which has a (non-index) field Val. I want a query to return the first record (in primary index order) within each Fld1 group where Val equals the maximum value within that Fld1 group.

I came up with this

select distinct * from
(select distinct * from Tbl group by Fld1, Val desc) Derived
group by Fld1;

This appears to work but, I'm now thinking it wouldn't work if the natural order of the records was different to primary index order. Is there an easier way?



Edited 1 time(s). Last edit at 05/17/2006 09:41AM by Tom Byars.

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.