SQL查询仅检索与列表或其子集匹配的记录

时间:2012-11-30 20:04:00

标签: sql sql-server

我正在研究从SCCM服务器分析添加/删除程序数据,并需要一些查询帮助。

已从SCCM检索数据并进行清理,以便我只留下我感兴趣的实际应用程序列表。常见的应用程序/安全更新/运行时等已被过滤掉。

我需要定义一个SQL查询,只有当它们有一个或多个由列表定义的应用程序时,它才会给我一个计算机及其应用程序的列表。

e.g。

Computer    | Application
--------------------------------------
WKS001      | Microsoft Office
WKS001      | WebEx
WKS001      | Java Runtime 1.6_33
--------------------------------------
WKS002      | Microsoft Office
WKS002      | WebEx
WKS002      | Java Runtime 1.6_33
WKS002      | Photoshop
--------------------------------------
WKS003      | WebEx
WKS003      | Java Runtime 1.6_33
--------------------------------------
WKS004      | WebEx
WKS004      | Photoshop

我需要检索所有具有以下任何或所有应用程序的计算机:

Microsoft Office
WebEx
Java Runtime 1.6_33

这应该给我WKS001WKS003

3 个答案:

答案 0 :(得分:1)

如果您需要排除具有不在该列表中的应用程序的计算机:

SELECT DISTINCT t1.Computer
FROM Table AS t1
LEFT OUTER JOIN Table AS t2 ON t1.Computer = t2.Computer
    AND t2.Application NOT IN ('Microsoft Office','WebEx','Java Runtime 1.6_33')
WHERE t1.Application IN ('Microsoft Office','WebEx','Java Runtime 1.6_33')
  AND t2.Computer IS NULL;

答案 1 :(得分:0)

SELECT Computer, Application
FROM TABLE_NAME_HERE
WHERE Application='Microsoft Office' 
   OR Application='WebEx' 
   OR Application='Java Runtime 1.6_33'

答案 2 :(得分:0)

其他答案不会带回计算机的应用程序。试试这个:

SELECT
    computer
    ,application
FROM
    TABLENAME 
WHERE
    computer IN
        (
            SELECT
                computer
            FROM
                TABLENAME
            WHERE
                application IN
                    (
                        'Microsoft Office'
                        ,'WebEx'
                        ,'Java Runtime 1.6_33'
                    )
        )
    AND comptuer NOT IN 
        (
            SELECT
                computer
            FROM
                TABLENAME
            WHERE
                application NOT IN
                    (
                        'Microsoft Office'
                        ,'WebEx'
                        ,'Java Runtime 1.6_33'
                    )
        )