title: Pivot a table and display n ordered time series
body: <p>I have a table named "readings" which contains data from sensors with the following columns:</p>
  
  <pre><code>id    id_device    date                   value
  1     1            2015-01-01 00:00:00    0.2
  2     2            2015-01-01 00:00:00    0.7
  3     1            2015-01-01 00:00:10    0.3
  4     2            2015-01-01 00:00:10    0.8
  5     1            2015-01-01 00:00:20    0.4
  6     2            2015-01-01 00:00:20    0.9
  </code></pre>
  
  <p>And I want to transform it to this table in a single query:</p>
  
  <pre><code>date                   device_id_1     device_id_2
  2015-01-01 00:00:00    0.2             0.7
  2015-01-01 00:00:10    0.3             0.8
  2015-01-01 00:00:20    0.4             0.9
  </code></pre>
  
  <p>I've found that doing this is called "pivoting a table" but I've only found how to sum values, not how to order them in time series</p>
  
  <p>I've tried </p>
  
  <pre><code>SELECT DISTINCT(date) FROM readings
  </code></pre>
  
  <p>and then inside the results loop, querying again for each sensor</p>
  
  <pre><code>SELECT value FROM readings WHERE date=$date AND id_device=$id
  </code></pre>
  
  <p>But I'm sure that this is not very efficient because it is a lot of queries (one query for each value)</p>
  
  <p>How the sql query would be like?</p>