title: SQL : Using GROUP BY and MAX on multiple columns
body: <p>I have an issue with an SQL Query. Lets take this example data</p>
  
  <pre><code>itemID  catID  attrib1  attrib2
    1       1       10       5   
    2       1       10       7
    3       1        5      10
    4       2       18      15
  </code></pre>
  
  <p>I want to return the best item for each category (with attrib1 having priority over attrib2)</p>
  
  <p>Obviously, <code>SELECT catID, MAX(attrib1), MAX(attrib2) FROM test_table GROUP BY catID</code> doesn't work since it will return 10 &amp; 10 for the 1st cat.</p>
  
  <p>So is there anyway to tell MySQL to select max value from attrib2 row but only consider the ones where attrib1 is also max value ? i.e return the following data</p>
  
  <pre><code> catID  attrib1  attrib2
     1       10       7   
     2       18      15
  </code></pre>