SQL相同的列数据

时间:2013-07-19 04:37:16

标签: sql

我有一张桌子 像

     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_Mem WinA3_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%肯定,我有170个实例的50K记录,我正在寻找这些服务器上常见的实例。

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

1 个答案:

答案 0 :(得分: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

Sample JSFiddle