2

我正在尝试查找以下设备:

  • 列 instancename 中不存在“PatchManagementPremium”
  • 列 instancename 中不存在“RemoteControl”
  • 列 instancename 中不存在这两个条目

但是我想排除两个条目都为真的所有结果,直到现在我才设法做到这一点。无论我尝试什么,当 1 和 2 为真时,它都表现为假...

SELECT DISTINCT 
    Devices.DeviceName
FROM
    Devices
LEFT OUTER JOIN 
    CustInv_ObjType_6121 BCMModulesVersions ON Devices.DeviceID = BCMModulesVersions.DeviceID
LEFT OUTER JOIN 
    InventoryIntegrationData InventoryUpdate ON Devices.DeviceID = InventoryUpdate.DeviceID
WHERE  
    ((BCMModulesVersions.InstanceName NOT IN ('PatchManagementPremium', 'RemoteControl'))
    AND (InventoryUpdate.IntegrationDate IS NOT NULL)
    AND Devices.TopologyType IN ('_DB_DEVTYPE_CLIENT_', '_DB_DEVTYPE_RELAY_'))
ORDER BY 
    Devices.DeviceName ASC; 

更清楚一点:我支持一个可以加载或不加载多个模块的应用程序。此信息存储在数据库中。加载模块后,您将在该设备的实例名称列中找到其名称(远程控制、补丁管理高级版等)。

我想列出所有未加载模块遥控器,或未加载模块补丁或未加载两个模块的设备。

如果两个条目都已加载,则设备名称不应出现在查询的输出中。

4

5 回答 5

1

如果我理解正确,您基本上可以这样做:

WHERE NOT(1 AND 2) AND (1 OR 2)
于 2016-11-02T14:56:29.363 回答
0

感谢 JamieD77,我找到了解决方案!:)

SELECT DISTINCT
        Devices.DeviceName
FROM    Devices
        JOIN InventoryIntegrationData InventoryUpdate ON Devices.DeviceID = InventoryUpdate.DeviceID
        JOIN CustInv_ObjType_6121 BCMModulesVersions ON Devices.DeviceID = BCMModulesVersions.DeviceID
WHERE   InventoryUpdate.IntegrationDate IS NOT NULL
        AND Devices.TopologyType IN ('_DB_DEVTYPE_CLIENT_','_DB_DEVTYPE_RELAY_')
        AND BCMModulesVersions.InstanceName NOT IN ('PatchManagement','RemoteControl')
        AND 2 > ( SELECT COUNT(DISTINCT BCMModulesVersions.InstanceName)
                         FROM   CustInv_ObjType_6121 BCMModulesVersions
                         WHERE  Devices.DeviceID = BCMModulesVersions.DeviceID
                                AND BCMModulesVersions.InstanceName IN ('PatchManagementPremium','RemoteControl'))
ORDER BY Devices.DeviceName ASC; 

感谢你的帮助!

于 2016-11-07T17:57:25.660 回答
0

我认为您是说这两种情况中的一种或两种都丢失了(因此排除了它们都存在)

SELECT DISTINCT 
    Devices.DeviceName
FROM
    Devices
LEFT OUTER JOIN 
    CustInv_ObjType_6121 BCMModulesVersions ON Devices.DeviceID = BCMModulesVersions.DeviceID
LEFT OUTER JOIN 
    InventoryIntegrationData InventoryUpdate ON Devices.DeviceID = InventoryUpdate.DeviceID
WHERE  

    (NOT EXISTS (SELECT 0 FROM CustInv_ObjType_6121 X WHERE X.InstanceName IN('RemoteControl'))
        OR
     NOT EXISTS (SELECT 0 FROM CustInv_ObjType_6121 Y WHERE Y.InstanceName IN('PatchManagementPremium'))
    )
    AND (InventoryUpdate.IntegrationDate IS NOT NULL)
    AND Devices.TopologyType IN ('_DB_DEVTYPE_CLIENT_', '_DB_DEVTYPE_RELAY_')
ORDER BY 
    Devices.DeviceName ASC; 
于 2016-11-02T15:39:44.680 回答
0

我想你也许可以在这里使用 NOT EXISTS。

SELECT DISTINCT
        Devices.DeviceName
FROM    Devices
        INNER JOIN InventoryIntegrationData InventoryUpdate ON Devices.DeviceID = InventoryUpdate.DeviceID
WHERE   InventoryUpdate.IntegrationDate IS NOT NULL
        AND Devices.TopologyType IN ('_DB_DEVTYPE_CLIENT_','_DB_DEVTYPE_RELAY_')
        AND NOT EXISTS ( SELECT 1
                         FROM   CustInv_ObjType_6121 BCMModulesVersions
                         WHERE  Devices.DeviceID = BCMModulesVersions.DeviceID
                                AND BCMModulesVersions.InstanceName IN ('PatchManagementPremium','RemoteControl'))
ORDER BY Devices.DeviceName ASC; 

如果您只想在两个值都存在时排除,您可以使用。

SELECT DISTINCT
        Devices.DeviceName
FROM    Devices
        JOIN InventoryIntegrationData InventoryUpdate ON Devices.DeviceID = InventoryUpdate.DeviceID
WHERE   InventoryUpdate.IntegrationDate IS NOT NULL
        AND Devices.TopologyType IN ('_DB_DEVTYPE_CLIENT_','_DB_DEVTYPE_RELAY_')
        AND 2 > ( SELECT COUNT(DISTINCT BCMModulesVersions.InstanceName)
                         FROM   CustInv_ObjType_6121 BCMModulesVersions
                         WHERE  Devices.DeviceID = BCMModulesVersions.DeviceID
                                AND BCMModulesVersions.InstanceName IN ('PatchManagementPremium','RemoteControl'))
ORDER BY Devices.DeviceName ASC; 
于 2016-11-02T15:34:56.763 回答
0

将过滤器移至HAVING子句并进行条件计数

这是一种方法

SELECT Devices.DeviceName
FROM   Devices
       JOIN CustInv_ObjType_6121 BCMModulesVersions
         ON Devices.DeviceID = BCMModulesVersions.DeviceID
       JOIN InventoryIntegrationData InventoryUpdate
         ON Devices.DeviceID = InventoryUpdate.DeviceID
WHERE  ( InventoryUpdate.IntegrationDate IS NOT NULL )
       AND Devices.TopologyType IN ( '_DB_DEVTYPE_CLIENT_', '_DB_DEVTYPE_RELAY_' )
GROUP  BY Devices.DeviceName
HAVING Count(CASE
               WHEN BCMModulesVersions.InstanceName IN ( 'PatchManagementPremium', 'RemoteControl' ) THEN 1
             END) = 0 
于 2016-11-02T14:56:36.607 回答