Select First Row In Each Group MySQL

Select the First Row In Each Group of MySQL

Here is an example:

SELECT  
      MIN(purchases2.id),  # change to MAX if you want the highest value
      purchases1.customer, 
      purchases1.total
FROM 
    PURCHASES purchases1
    JOIN (SELECT p.customer,
                 MAX(total) AS max_total
            FROM PURCHASES purchases
            GROUP BY p.customer) purchases2 
   ON purchases2.customer = purchases1.customer
      AND purchases2.max_total = purchases1.total
GROUP BY purchases1.customer, purchases1.total
Please follow and like us:
Content Protection by DMCA.com