-2

我有一张像

     Server           Instance
MURDW02D    WinA3_NetInstances
MURDW02D    WinA3_MemVirt
MURAW06D    WinA3_CPU
MURDW02D    WinA3_NetInst
MURDW02D    WinA3_CPUInst
MURAW06D    WinA3_Net
MURDW02D    WinA3_Mem
MURDW01D    WinA3_Mem
MURDW01D    WinA3_CPUInst
MURDW02D    WinA3_Net
MURDW02D    WinA3_CPUInst
MURAW07D    WinA3_Net

我想选择 MURDW01D 和 MURDW02D ,因为它们有WinA3_MemWinA3_CPUInst通用。可能有 3 个常见的字段。另外,我想要有这个共同点的服务器的数量是 2 在这种情况下是可能的。

查询已尝试

SELECT * FROM (SELECT HOSTNAME AS [Host], CLASS AS [class] FROM oncall) AS s PIVOT(COUNT(CLASS) FOR [CLASS] IN (WinA3_Proc)) AS pivot

这可能不是 100% 确定的,我有 50K 记录和 170 个实例,我正在寻找这些服务器上常见的实例。

SELECT COUNT(*) AS Expr1
FROM oncall
WHERE (CLASS LIKE '%WinA3%') AND (CLASS LIKE '%WinA3_CPUInst%')
4

1 回答 1

1
declare @t as table (Servr varchar(max), INstance varchar(max))

insert into @t values ('MURDW02D',    'WinA3_NetInstances')
insert into @t values ('MURDW02D',    'WinA3_MemVirt')
insert into @t values ('MURAW06D',    'WinA3_CPU')
insert into @t values ('MURDW02D',    'WinA3_NetInst')
insert into @t values ('MURDW02D',    'WinA3_CPUInst')
insert into @t values ('MURAW06D',    'WinA3_Net')
insert into @t values ('MURDW02D',    'WinA3_Mem')
insert into @t values ('MURDW01D',   'WinA3_Mem')
insert into @t values ('MURDW01D',    'WinA3_CPUInst')
insert into @t values ('MURDW02D',    'WinA3_Net')
insert into @t values ('MURDW02D',    'WinA3_CPUInst')
insert into @t values ('MURAW07D',    'WinA3_Mem')

-- Get the servers and their counts which have both WinA3_Mem and WinA3_CPUInst
Select servr, count(*) serverCount from
(Select distinct servr, Instance from @t where instance LIKE 'WinA3_Mem%'
 union
Select distinct servr, Instance from @t where instance LIKE 'WinA3_CPUInst%') X
group by servr
having count(*) > 1

示例 JSFiddle

于 2013-07-19T05:09:18.470 回答