title: postgres: get top n occurrences of a value within each group
body: <p>I have a simple table like this:</p>
  
  <pre><code>user    letter
  --------------
  1       A
  1       A
  1       B
  1       B
  1       B
  1       C
  
  2       A
  2       B
  2       B
  2       C
  2       C
  2       C
  </code></pre>
  
  <p>I want to get the top 2 occurrences of 'letter' per user, like so</p>
  
  <pre><code>user    letter  rank(within user group)
  --------------------
  1       B       1
  1       A       2
  
  2       C       1
  2       B       2
  </code></pre>
  
  <p>or even better: collapsed into columns</p>
  
  <pre><code>user    1st-most-occurrence  2nd-most-occurrence
  1       B                   A
  2       C                   B
  </code></pre>
  
  <p>How can I accomplish this in postgres?</p>