I have the following table
+--------+-------------+-------------+-------------------------+-----------------+
| Server | DriveLetter | SampleValue | Timestamp | CounterName |
+--------+-------------+-------------+-------------------------+-----------------+
| srv1 | C: | 3357 | 2014-05-23 12:15:37.000 | Free Megabytes |
| srv1 | C: | 25000 | 2014-05-23 11:49:19.963 | Total Disk Size |
| srv1 | D: | 87183 | 2014-05-23 12:45:37.000 | Free Megabytes |
| srv1 | D: | 165725 | 2014-05-23 11:49:19.963 | Total Disk Size |
| srv2 | C: | 61351 | 2014-05-23 12:17:17.000 | Free Megabytes |
| srv2 | C: | 104900 | 2014-05-23 11:07:21.643 | Total Disk Size |
| srv2 | F: | 150918 | 2014-05-23 12:17:17.000 | Free Megabytes |
| srv2 | F: | 370880 | 2014-05-23 11:07:21.643 | Total Disk Size |
+--------+-------------+-------------+-------------------------+-----------------+
I now need to pivot this table into the following
+--------+-------------+----------------+----------------------------+-----------------+-----------------------------+---------------------+
| Server | DriveLetter | Free Megabytes | Timestamp (Free Megabytes) | Total Disk Size | Timestamp (Total Disk Size) | % Free (Calculated) |
+--------+-------------+----------------+----------------------------+-----------------+-----------------------------+---------------------+
| srv1 | C: | 3357 | 2014-05-23 12:15:37.000 | 25000 | 2014-05-23 11:49:19.963 | 13,428 |
| srv1 | D: | 87183 | 2014-05-23 12:45:37.000 | 165725 | 2014-05-23 11:49:19.963 | 52.607 |
| srv2 | C: | 61351 | 2014-05-23 12:17:17.000 | 104900 | 2014-05-23 11:07:21.643 | 58.485 |
| srv2 | F: | 150918 | 2014-05-23 12:17:17.000 | 370880 | 2014-05-23 11:07:21.643 | 40.691 |
+--------+-------------+----------------+----------------------------+-----------------+-----------------------------+---------------------+
I created a PIVOT Query but now I got stuck while trying to attach the Timestamp to the value. I also have no clue how to force a calculation into a new column.
SELECT
Server, DriveLetter, [Free Megabytes], [Total Disk Size]
FROM
(SELECT
Server, DriveLetter, CounterName, SampleValue
FROM
dbo.DiskInfo_LastCheck) AS d
PIVOT (MAX(SampleValue) FOR CounterName in ([Free Megabytes], [Total Disk Size])) as P
ORDER BY Server
Sorry I'm a total SQL newbie...