title: Using PIVOT in SQL Server 2008
body: <p>Let's say I have some data, either in a SQL Server 2008 table or a [table]-typed variable:</p>
  
  <pre><code>author_id     review_id     question_id     answer_id
  88540         99001         1               719
  88540         99001         2               720
  88540         99001         3               721
  88540         99001         4               722
  88540         99001         5               723
  36414         24336         1               302
  36414         24336         2               303
  36414         24336         3               304
  36414         24336         4               305
  36414         24336         5               306
  </code></pre>
  
  <p>I want to retrieve the data as a result set that looks like this:</p>
  
  <pre><code>author_id     review_id     1     2     3     4     5
  88540         99001         719   720   721   722   723
  36414         24336         302   303   304   305   306
  </code></pre>
  
  <p>I suspect the PIVOT operator is what I need (according to <a href="https://stackoverflow.com/questions/1461489/best-way-to-flatten-denormalize-sql-lookup-tables">this post</a>, anyway), but I can't figure out how to get started, especially when the number of <strong>question_id</strong> rows in the table can vary.  In the above example, it's 5, but in another query the table might be populated with 7 distinct questions.</p>