我正在尝试为Windows性能监视器创建一个视图。我遇到的问题是,我称为“ AllPerf”的表具有名为“ PerfCounter”和“应用程序”的两列,其中具有性能计数器和应用程序的几种不同名称。
理想情况下,我想要的列是:时间,计算机,应用程序名称,然后是列中所有PerfCounter行的名称。
我仅创建了所需行名的视图,并在下面创建的当前视图下得到以下输出:
-----------------------------------------------------------------------
| mem.AlertTime | Computer | PerfCounter | Application | Value |
-----------------------------------------------------------------------
| 2019-03-15 14:49:02 | WEB-04 | Vrt_Bytes | System |0.1368 |
| 2019-03-15 14:49:02 | WEB-05 | Vrt_Bytes | System |2440 |
| 2019-03-15 14:49:02 | WEB-06 | Handles | w3wp |1508 |
| 2019-03-15 14:49:02 | WEB-04 | Page_Faults | System |0.00419 |
| 2019-03-15 14:49:02 | WEB-04 | Prvt_Bytes | System |0.1368 |
-----------------------------------------------------------------------
我在下面的链接中尝试了解决方案,但是当我可以成功地将行名列为列时,列下没有填充值。 Efficiently convert rows to columns in sql server
而且由于我一般没有太多SQL经验,所以我似乎无法用更复杂的数据来推断简单的示例
这就是我用于当前视图的SELECT语句的内容。
SELECT mem.AlertTime,
Computer,
CASE WHEN mem.PerfCounter = 'Virtual Bytes' THEN 'Virt_Bytes'
WHEN mem.PerfCounter = 'Private Bytes' THEN 'Prvt_Bytes'
WHEN mem.PerfCounter = 'Page Faults/sec' THEN 'Page_Faults_Sec'
WHEN mem.PerfCounter = 'Thread Count' THEN 'Threads'
WHEN mem.PerfCounter LIKE '%Handle%' THEN 'Handles'
END AS PerfCounter,
PerfInstance AS Application,
Value
FROM dbo.AllPerf AS mem
我想要的是这样的:
--------------------------------------------------------------------------------------------
| mem.AlertTime | Computer |Application| Vrt_Bytes| Prvt_Bytes| Handles| Page_Faults |
-------------------------------------------------------------------------------------------
| 2019-03-15 14:49:02 | WEB-04 | System | 12440 | 24.13 | 13 | 0.14 |
| 2019-03-15 14:49:02 | WEB-04 | w3wp | 7396 | 4.2309 | 13 | 0 |
| 2019-03-15 14:49:02 | WEB-05 | w3wp | 1538 | 0.1368 | 1538 | 0 |
| 2019-03-15 14:49:02 | WEB-05 | System | 6629 | 6500 | 1835 | 5 |
| 2019-03-15 14:49:02 | WEB-06 | System | 2440 | 0.1368 | 13 | 0 |
--------------------------------------------------------------------------------------------
如果我希望在天空中飞奔,我会将Mem_Bytes下的MBytes隐瞒到GB,但是我无法成功创建CASE语句并将数学包括在结果中
答案 0 :(得分:0)
我创建了一个变量表@MYTAB,用于测试下面的脚本
您可以用帖子中使用的整个视图替换@MYTAB
DECLARE @MYTAB AS table(AlertTime datetime,Computer varchar(50),PerfCounter varchar(50),Application varchar(50),Value decimal(10,5))
insert into @mytab
values('2019-03-15 14:49:02','WEB-04' , 'Vrt_Bytes', 'System' ,'0.1368') ,
('2019-03-15 14:49:02' , 'WEB-05' , 'Vrt_Bytes' , 'System' ,2440 ),
('2019-03-15 14:49:02' , 'WEB-06' , 'Handles' , 'w3wp' ,1508 ),
('2019-03-15 14:49:02' , 'WEB-04' , 'Page_Faults' , 'System' ,0.00419 ),
('2019-03-15 14:49:02' , 'WEB-04' , 'Prvt_Bytes' , 'System' ,0.1368 )
select alerttime,computer,application,piv.*
from @MYTAB d
pivot
( max(value) for perfcounter in ([vrt_bytes],[handles],[page_faults]) ) piv