title: Transposing an sql result so that one column goes onto multiple columns
body: <p>I'm trying to get data out of a table for a survey in a particular format. However all my attempts seems to hand the DB because of too many joins/too heavy on the DB.</p>
  
  <p>My data looks like this:</p>
  
  <pre><code>id, user, question_id, answer_id, 
  1,   1,   1,           1
  3,   1,   3,           15
  4,   2,   1,           2
  5,   2,   2,           12
  6,   2,   3,           20
  </code></pre>
  
  <p>There are roughly 250,000 rows and each user has about 30 rows. I want the result to look like:</p>
  
  <pre><code>user0, q1, q2,   q3 
  1,     1,  NULL, 15
  2,     2,  12,   20 
  </code></pre>
  
  <p>So that each user has one row in the result, each with a separate column for each answer.</p>
  
  <p>I'm using Postgres but answers in any SQL language would be appreciated as I could translate to Postgres.</p>
  
  <p>EDIT: I also need to be able to deal with users not answering questions, i.e. in the example above q2 for user 1.</p>