MySQL Forums
Forum List  »  General

Re: selecting distinct records with group max value
Posted by: Peter Brawley
Date: May 17, 2006 01:45PM

Wouldn't that just be...

DROP TEMPORARY TABLE IF EXISTS tmp;
SET @prev = -1;
CREATE TEMPORARY TABLE tmp
SELECT
fld1, fld2, fld3, val,
IF( fld1 = @prev, 0, @prev := fld1 ) AS Sel
FROM tbl
ORDER BY fld1 ASC, val DESC;

SELECT fld1, fld2, fld3, val FROM tmp WHERE sel > 0;

DROP TEMPORARY TABLE tmp;

PB

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.