title: How to select the maximum of each type
body: <p>I have a table that describes how many FISH, VEG, OR meat are in each tray.</p>
  
  <pre><code> tray  | qty | type
  -------+-----+-------
       1 |   5 | FISH
       2 |   6 | VEG
       2 |   2 | FISH
       2 |   5 | MEAT
       3 |   8 | VEG
       3 |   3 | FISH
       3 |   9 | MEAT
       4 |  10 | MEAT
  </code></pre>
  
  <p>Lets call this table R (it is a sub table that I created in my query).</p>
  
  <p>What I want is a table that says which tray has the highest number of each type like this:</p>
  
  <pre><code>  type | tray
  -------+------
    FISH |  1 
     VEG |  3
    MEAT |  4
  </code></pre>
  
  <p>I tried to write the following query</p>
  
  <pre><code>SELECT type AS type1, tray
  FROM (SELECT ... bla bla) AS R
  WHERE R.qty IN (SELECT MAX(qty) FROM R WHERE type = type1);
  </code></pre>
  
  <p>the error is that R doesn't exist, how do I solve this?</p>