SQL子查询/连接

时间:2015-09-15 19:20:13

标签: sql sql-server

我正在尝试使用子查询/连接来将表中的一组行与同一表中的其他行进行比较。

摘录数据集:

Guid                        MonitorsetID    groupName
26464009405210800000000000  2162            ~templates.root
26464009405210800000000000  2161            ~templates.root
26464009405210800000000000  1464            ~templates.root
26464009405210800000000000  1224            ~templates.root
321794737607583             2162            lab.root.abc
321794737607583             2161            lab.root.abc
321794737607583             1464            lab.root.abc
321794737607583             1224            lab.root.abc
500311571061532             2196            lab.root.abc
500311571061532             2195            lab.root.abc
500311571061532             1464            lab.root.abc
500311571061532             1224            lab.root.abc
129478194721498             1464            lab.root.def
129478194721498             1224            lab.root.def

我需要在一个特定的Guid'26464009405210800000000000'上存在MonitorsetID,但是对于其他Guid记录的选择组是“缺失”的,例如所有Guids都有groupName'lab.root.abc'。在上面的exerpt中,目前有4个MonitoretID与这个Guid相匹配:2162,2161,1464,1224。2162和2161从Guid'500311571061532'“失踪”。

我想要的结果集是:

Guid                        MonitorsetID    groupName
500311571061532             2162            lab.root.abc
500311571061532             2161            lab.root.abc

或者,以下内容也适用:

Guid                        MonitorsetID    groupName           Guid                MonitorsetID    groupName
26464009405210800000000000  2162            ~templates.root     500311571061532     NULL            lab.root.abc
26464009405210800000000000  2161            ~templates.root     500311571061532     NULL            lab.root.abc

我可以通过以下方式获得我想要的结果的反转:

SELECT VMAA.agentguid, VMAA.MonitorsetID
FROM [vMonitorsetAgentAssignment] VMAA
LEFT JOIN [vMonitorsetAgentAssignment] VMAA2
ON VMAA.MonitorsetID = VMAA2.MonitorsetID

WHERE
    VMAA.agentguid in 
    (
    SELECT AgentGuid FROM vMonitorsetAgentAssignment VMAA
    WHERE VMAA.groupName = 'lab.root.abc'
    )
    AND
    VMAA2.agentguid = '26464009405210876452365122'

ORDER BY agentGuid, MonitorsetID

我尝试通过向“IS NULL”等的JOIN ON子句添加条件来获取所需结果,只是返回空白结果。

4 个答案:

答案 0 :(得分:1)

获取要过滤的groupName中的GUID的所有可能组合以及要过滤的GUID的MonitorsetID(通过从基表交叉连接到GUID的一组不同的MonitorsetID)你正在过滤)。

一旦你拥有了这个数据集,它就会成为一个简单的左连接,从那个派生数据集再次转移到你的基表上guid / monitersetIds不存在:

with root_data as
(
    select  distinct a.agentguid, b.MonitorsetID, a.groupName
    from    vMonitorsetAgentAssignment a
    cross join (
            select  distinct MonitorsetID
            from    vMonitorsetAgentAssignment d
            where   d.agentguid = '26464009405210800000000000'
        ) b
    where   a.groupName = 'lab.root.abc'
)
select  *
from    root_data d
left join vMonitorsetAgentAssignment a
on      a.agentguid = d.agentguid
and     a.MonitorsetID = d.MonitorsetID
where   a.agentguid is null;

答案 1 :(得分:0)

如果我理解正确:

select '500311571061532' as agentguid, a.MonitorsetID
from vMonitorsetAgentAssignment a left join
     vMonitorsetAgentAssignment a2
     on a2.agentguid = '500311571061532' and
        a.MonitorsetID = a2.MonitorsetID
where a.agentguid = '321794737607583' and
      a2.agentguid is null;

我不确定小组的来源。

答案 2 :(得分:0)

此查询将为guids 129478194721498和500311571061532提取缺少的ID。

with createddata as (
  select *
  from
  (
     select distinct guid, groupname from vMonitorsetAgentAssignment 
     where guid <> '26464009405210800000000000'
  ) a
  cross join
  (
     select monitorsetid from vMonitorsetAgentAssignment 
     where guid = '26464009405210800000000000'
  ) b
)
select x.*
from createddata x
left join vMonitorsetAgentAssignment y
  on x.guid = y.guid
  and x.monitorsetid = y.monitorsetid
  and x.groupname = y.groupname
  and y.guid <> '26464009405210800000000000'
where y.guid is null

Results
|            guid |    groupname | monitorsetid |
|-----------------|--------------|--------------|
| 129478194721498 | lab.root.def |         2162 |
| 129478194721498 | lab.root.def |         2161 |
| 500311571061532 | lab.root.abc |         2162 |
| 500311571061532 | lab.root.abc |         2161 |

示例:http://sqlfiddle.com/#!3/80719/33

忽略下面的答案

请查看此查询以获得所需的结果:

  select a.* from 
  vMonitorsetAgentAssignment a
  left join (
      select monitorsetid as masterids from vMonitorsetAgentAssignment 
      where guid = '26464009405210800000000000'
  ) b on a.monitorsetid = b.masterids
  where not guid = '26464009405210800000000000'
        and masterids is null

Result:
|            Guid | MonitorsetID |    groupName |
|-----------------|--------------|--------------|
| 500311571061532 |         2196 | lab.root.abc |
| 500311571061532 |         2195 | lab.root.abc |

答案 3 :(得分:0)

这基本上就是你所需要的。

WITH monitor_sets AS (
    SELECT MonitorsetID, agentguid
    FROM vMonitorsetAgentAssignment
    WHERE agentguid = '26464009405210876452365122'
)

SELECT DISTINCT vmaa.agentguid AS guid_with_missing, ms.agentguid, ms.MonitorsetID
FROM vMonitorsetAgentAssignment vmaa
CROSS JOIN monitor_sets ms
WHERE vmaa.groupName = 'lab.root.abc' 
    AND vmaa.MonitorsetID NOT IN (SELECT MonitorsetID FROM monitor_sets)
    AND vmaa.agentguid NOT IN (SELECT agentguid FROM vMonitorsetAgentAssignment WHERE MonitorsetID = ms.MonitorsetID)