Stack Overflow的下午好人。
我正在努力做一些我不确定如何用我的SQL知识实现的东西,但我清楚地知道我想要的是什么,所以希望这对人们有意义。
我有一些如下所示的perfmon磁盘结果:
DatabaseName ObjectName CounterName InstanceName Server Average
DAG1DB01 logicaldisk avg. disk sec/read d:\mountpoints\DAG1DB01 Server1 13.616
DAG1DB01 logicaldisk avg. disk sec/read d:\mountpoints\DAG1DB01 Server2 17.508
DAG1DB01 logicaldisk avg. disk sec/read d:\mountpoints\DAG1DB01 Server3 12.775
DAG1DB01 logicaldisk avg. disk sec/read d:\mountpoints\DAG1DB01 Server4 13.148
DAG1DB01 logicaldisk avg. disk sec/read d:\mountpoints\DAG1DB01 Server5 10.091
这些是基于此查询的示例行:
SELECT
e14_db.DatabaseName AS [DatabaseName],
d.ObjectName,
d.CounterName,
d.InstanceName,
d.Server,
AVG(Value) * 1000 AS [Average]
FROM E14_Perfmon_MBX AS d
INNER JOIN E14_Databases AS e14_db
ON e14_db.LogFolderPath = d.InstanceName
WHERE d.ObjectName = 'logicaldisk'
AND d.CounterName = 'avg. disk sec/read'
AND d.DateTime > (DATEADD(hh, -4, GETDATE()))
AND d.Value < 1
GROUP BY d.ObjectName, d.CounterName, d.InstanceName, d.Server, e14_db.DatabaseName
ORDER BY e14_db.DatabaseName, d.Server
我想要的是:
DatabaseName ObjectName CounterName InstanceName Server1 Server2 Server3 Server4 Server5
DAG1DB01 logicaldisk avg. disk sec/read d:\mountpoints\DAG1DB01 13.616 17.508 12.775 13.148 10.091
有谁知道我怎么能做到这一点?基本上把行应用到自己身上?
如果需要澄清,请随时提出。
谢谢!
答案 0 :(得分:1)
PIVOT
运算符专为此类情况而设计。这应该可以满足您的需求:
SELECT DatabaseName,
ObjectName,
CounterName,
InstanceName,
Server1,
Server2,
Server3,
Server4,
Server5
FROM (SELECT
e14_db.DatabaseName AS [DatabaseName],
d.ObjectName,
d.CounterName,
d.InstanceName,
d.[Server],
d.Value * 1000 AS [Value]
FROM E14_Perfmon_MBX AS d
INNER JOIN E14_Databases AS e14_db
ON e14_db.LogFolderPath = d.InstanceName
WHERE d.ObjectName = 'logicaldisk'
AND d.CounterName = 'avg. disk sec/read'
AND d.DateTime > (DATEADD(hh, -4, GETDATE()))
AND d.Value < 1) sub
PIVOT
(
AVG(Value)
FOR [Server] IN ([Server1], [Server2], [Server3], [Server4], [Server5])
) AS PivotTable
答案 1 :(得分:1)
以下语句计算固定列数(从1到5)中每个服务器的单独平均值:
SELECT e14_db.DatabaseName AS [DatabaseName]
, d.ObjectName, d.CounterName, d.InstanceName
, AVG(CASE WHEN d.Server = 'Server1' THEN Value ELSE NULL END) *1000 AS [Server1]
, AVG(CASE WHEN d.Server = 'Server2' THEN Value ELSE NULL END) *1000 AS [Server2]
, AVG(CASE WHEN d.Server = 'Server3' THEN Value ELSE NULL END) *1000 AS [Server3]
, AVG(CASE WHEN d.Server = 'Server4' THEN Value ELSE NULL END) *1000 AS [Server4]
, AVG(CASE WHEN d.Server = 'Server5' THEN Value ELSE NULL END) *1000 AS [Server5]
FROM E14_Perfmon_MBX AS d
INNER JOIN E14_Databases AS e14_db
ON e14_db.LogFolderPath = d.InstanceName
WHERE d.ObjectName = 'logicaldisk'
AND d.CounterName = 'avg. disk sec/read'
AND d.DateTime > (DATEADD(hh, -4, GETDATE()))
AND d.Value < 1
GROUP BY d.ObjectName, d.CounterName, d.InstanceName, e14_db.DatabaseName
ORDER BY e14_db.DatabaseName
您也可以使用PIVOT
,但更广泛的SQL 风格支持此语法。
答案 2 :(得分:1)
方法#1
declare @Servers varchar(1000) = ''
declare @Query varchar(8000) = ''
select @Servers = ISNULL(QuoteName(Server) + ',', '') + @Servers from MyTable
set @Servers = SUBSTRING(@Servers,0,len(@Servers))
Set @Query = 'Select DatabaseName, obectName, CounterName, InstanceName , ' +
@Servers + ' From
(
Select DatabaseName, obectName, CounterName, InstanceName,
Server, Average from Mytable
)K Pivot
(
max(Average) FOR Server IN (' + @Servers + ')
) AS pvt'
EXEC(@Query)
方法#2
Select DatabaseName, obectName, CounterName, InstanceName , Server1, Server2,
Server3, Server4, Server5
From
(
Select Select DatabaseName, obectName, CounterName, InstanceName,
Server, Average from Mytable FROM Mytable
)K
Pivot
(
Max(Average) FOR Server IN (Server1, Server2, Server3, Server4, Server5)
) AS pvt
答案 3 :(得分:0)
尝试为每个服务器加入一次PERFMON表,如下所示:
FROM E14_Databases AS e14_db
INNER JOIN E14_Perfmon_MBX AS server1
ON e14_db.LogFolderPath = server1.InstanceName
AND server1.Server = 'Server1'
AND server1.ObjectName = 'logicaldisk'
AND server1.CounterName = 'avg. disk sec/read'
AND server1.DateTime > (DATEADD(hh, -4, GETDATE()))
AND server1.Value < 1
INNER JOIN E14_Perfmon_MBX AS server2
ON e14_db.LogFolderPath = server2.InstanceName
AND server2.Server = 'Server2'
AND server2.ObjectName = 'logicaldisk'
AND server2.CounterName = 'avg. disk sec/read'
AND server2.DateTime > (DATEADD(hh, -4, GETDATE()))
AND server2.Value < 1
然后为每个连接添加一列:
AVG(Server1.Value) * 1000 AS [Server1]
答案 4 :(得分:-1)
答案 5 :(得分:-3)
修改表以包含额外列,并运行更新其中server ='Server2'
更新d set server2 =先前服务器条目的值(例如,server ='server2')
这样做很痛苦,但唯一的另一个选择是在不使用SQL语句的情况下物理编辑表。
编辑:我建议制作2个表,一个包含所有信息(数据库名称等),然后一个包含分数