title: Reshaping data in SQL
body: <p>I have a simple table like</p>
  
  <pre><code>time_stamp, metric, value
  2012/3/2  , x     , 1
  2012/3/2  , y     , 1.6
  2012/3/3  , x     , 0
  2012/3/3  , y     , null
  ...
  </code></pre>
  
  <p>and I want to write a query that returns something like</p>
  
  <pre><code>time_stamp, x, y
  2012/3/2  , 1, 1.6
  2012/3/3  , 0, null    
  </code></pre>
  
  <p>In other words, basically generate columns based on unique values of one field on the fly and reshape data into it.</p>
  
  <p>I remember I have seen it somewhere, but I just cannot find it now.</p>
  
  <p>PS. I am using PostgreSQL as of now. Please note if the solution is specific to a particular DBMS.</p>
  
  <p>Updates: The values of the metric, from which columns are being formed are dynamic. I understand that makes this harder though and a single query solution almost impossible.</p>
  
  <p>As per comments, I have tried crosstab as well and it is a) very pg specific and b) not so nice with dynamic number of tables.</p>