Mon, 17 Dec 2012

How To Select The Top Category From A Table Sorted By A Column in MySQL

This seems easy (and arguably is easy) until you're faced with it and have to do some research.

To assist those who have a similar problem (you want the top representative for a given category, ordered by a numerical value), here was my solution:
SELECT DISTINCT category, name, MAX( value ) FROM ( SELECT * FROM f ORDER BY value DESC ) t1 GROUP BY category;

Khan Klatt

Khan Klatt's photo