T-SQL: from rows to columns but not an actual pivot

Posted by Matte on Stack Overflow See other posts from Stack Overflow or by Matte
Published on 2012-12-05T16:39:28Z Indexed on 2012/12/05 17:03 UTC
Read the original article Hit count: 209

Is there a T-SQL (SQL Server 2008R2) query to transform TABLE_1 into the expected resultset?

TABLE_1

+----------+-------------------------+---------+------+
| IdDevice | Timestamp               | M300    | M400 |
+----------+-------------------------+---------+------+
| 3        | 2012-12-05 16:29:51.000 | 2357,69 | 520  |
| 6        | 2012-12-05 16:29:51.000 | 1694,81 | 470  |
| 1        | 2012-12-05 16:29:51.000 | 2046,33 | 111  |
+----------+-------------------------+---------+------+

Expected resultset

+-------------------------+---------+--------+---------+--------+---------+--------+
|        Timestamp        | 3_M300  | 3_M400 | 6_M300  | 6_M400 | 6_M300  | 6_M400 |
+-------------------------+---------+--------+---------+--------+---------+--------+
| 2012-12-05 16:29:51.000 | 2357,69 |    520 | 1694,81 |    470 | 2046,33 |    111 |
+-------------------------+---------+--------+---------+--------+---------+--------+

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about sql-server-2008