title: SQL Transpose Rows as Columns
body: <p>I have an interesting conundrum which I believe can be solved in purely SQL. I have tables similar to the following:</p>
  
  <pre><code>responses:
  
  user_id | question_id | body
  ----------------------------
  1       | 1           | Yes
  2       | 1           | Yes
  1       | 2           | Yes
  2       | 2           | No
  1       | 3           | No
  2       | 3           | No
  
  
  questions:
  
  id | body
  -------------------------
  1 | Do you like apples?
  2 | Do you like oranges?
  3 | Do you like carrots?
  </code></pre>
  
  <p>and I would like to get the following output</p>
  
  <pre><code>user_id | Do you like apples? | Do you like oranges? | Do you like carrots?
  ---------------------------------------------------------------------------
  1       | Yes                 | Yes                  | No
  2       | Yes                 | No                   | No
  </code></pre>
  
  <p>I don't know how many questions there will be, and they will be dynamic, so I can't just code for every question. I am using PostgreSQL and I believe this is called transposition, but I can't seem to find anything that says the standard way of doing this in SQL. I remember doing this in my database class back in college, but it was in MySQL and I honestly don't remember how we did it.</p>
  
  <p>I'm assuming it will be a combination of joins and a <code>GROUP BY</code> statement, but I can't even figure out how to start.</p>
  
  <p>Anybody know how to do this? Thanks very much!</p>
  
  <p><strong>Edit 1:</strong> I found some information about using a <a href="http://www.postgresql.org/docs/current/static/tablefunc.html" rel="noreferrer">crosstab</a> which seems to be what I want, but I'm having trouble making sense of it. Links to better articles would be greatly appreciated!</p>