SQL query for retrieving different data series contained in the same table <p>I'm trying to retrieve data series contained in a table that basically looks like this:</p> <pre><code>row | timestamp | seriesId | int32 | int64 | double --------------------------------------------------- 0 | 0 | 0 | 2 | | 1 | 1 | 0 | 4 | | 2 | 1 | 1 | 435 | | 3 | 1 | 2 | | 2345 | 4 | 1 | 3 | | | 0.5 5 | 2 | 0 | 5 | | 6 | 2 | 1 | 453 | | 7 | 2 | 2 | | 2401 | .... </code></pre> <p>I would like to get a result set that looks like this (so that I can easily plot it):</p> <pre><code>row | timestamp | series0 | series1 | series 2 | ... ---------------------------------------------------- 0 | 0 | 2 | | | 1 | 1 | 4 | 435 | 2345 | 2 | 2 | 5 | 453 | 2401 | ... </code></pre> <p>My SQL skillz are unfortunately not really what they should be, so my first attempt at achieving this feels a bit awkward:</p> <pre><code>SELECT tbl0.timestamp, tbl0.int32 as series0, tbl1.int32 as series1 FROM (SELECT * FROM StreamData WHERE seriesId=0) as tbl0 INNER JOIN (SELECT * FROM StreamData WHERE seriesId=1) as tbl1 ON tbl0.timestamp = tbl1.timestamp ORDER BY tbl0.timestamp; </code></pre> <p>This doesn't really seem to be the right way of trying to accomplish this, especially not when the number of different series goes up. I can change the way data gets stored in the table (it's in an SQLite database if that matters) if that would make things easier, but as the number of different series may be different from time to time, I would prefer having them all in the same table.</p> <p><strong>Is there a better way to write the above query?</strong></p>