SQL Server:PIVOT时间戳并计算

时间:2014-05-23 13:54:35

标签: sql sql-server pivot pivot-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 |
+--------+-------------+-------------+-------------------------+-----------------+

我现在需要将此表格转换为以下内容

+--------+-------------+----------------+----------------------------+-----------------+-----------------------------+---------------------+
| 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              |
+--------+-------------+----------------+----------------------------+-----------------+-----------------------------+---------------------+

我创建了一个PIVOT查询,但现在我在尝试将时间戳附加到值时遇到了问题。我也不知道如何强制计算到新列。

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

抱歉,我是一个全新的SQL新手...

1 个答案:

答案 0 :(得分:1)

我建议使用条件聚合而不是pivot来执行此操作:

select lc.server, lc.DriveLetter
       max(case when CounterName = 'Free Megabytes' then SampleValue end) as [Free Megabytes],
       max(case when CounterName = 'Free Megabytes' then TimeStamp end) as [TimeStamp Free Megabytes],
       max(case when CounterName = 'Total Disk Size' then SampleValue end) as [Total Disk]
       max(case when CounterName = 'Total Disk Size' then TimeStamp end) as [TimeStamp Total Disk],
       (100 * max(case when CounterName = 'Free Megabytes' then cast(SampleValue as float) end) /
        max(case when CounterName = 'Total Disk Size' then SampleValue end)
       ) as [Percent Free]
from dbo.DiskInfo_LastCheck lc
group by lc.server, lc.DriveLetter;