Created
November 29, 2012 11:37
-
-
Save ThomasWeinert/4168416 to your computer and use it in GitHub Desktop.
SQL Maximum Of Group (latest ten, but only one of each group)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT * | |
FROM mytable | |
WHERE idfield IN ( | |
SELECT MAX(main.idfield) | |
FROM mytable AS main | |
JOIN ( | |
SELECT groupfield, MAX(maxfield) maximum | |
FROM mytable | |
GROUP BY groupfield | |
ORDER BY maximum DESC | |
LIMIT 10 | |
) AS selector | |
ON ( | |
main.groupfield = selector.groupfield | |
AND | |
main.maxfield = selector.maximum | |
) | |
GROUP BY main.groupfield | |
) | |
ORDER BY maxfield DESC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment