显示连接到其他数据的唯一SQL记录

时间:2017-02-09 16:58:45

标签: sql sql-server

我正在尝试编写SQL来检索我环境中所有Windows 10设备的列表,以及它们是否安装了Acrobat Reader。

我希望每个设备只返回一行,并且“是”或“否”表示是否已安装Reader。

到目前为止,我有:

SELECT distinct
    SV.Netbios_Name0,
CASE
    WHEN ARP.DisplayName0 LIKE '%Acrobat Reader%' THEN
        'Yes'
    ELSE
        'No'
    END as 'Has Reader',
    ARP.DisplayName0
FROM 
    dbo.v_R_System_Valid as SV
    JOIN dbo.v_Add_Remove_Programs as ARP
        on SV.ResourceID = ARP.ResourceID
    JOIN dbo.v_GS_OPERATING_SYSTEM as OS
        on SV.ResourceID = OS.ResourceID
WHERE
    OS.Caption0 LIKE '%Windows 10%'
GROUP BY
    SV.Netbios_Name0,
    ARP.DisplayName0,
    OS.Caption0

问题是每个设备上的每个已安装应用程序都会返回一行:

SQL Results

谢谢!

1 个答案:

答案 0 :(得分:2)

您需要从DisplayName0(以及GROUP BY)中删除SELECT列。这应该做:

SELECT 
    SV.Netbios_Name0,
    MAX(CASE
            WHEN ARP.DisplayName0 LIKE '%Acrobat Reader%' THEN
                'Yes'
            ELSE
                'No'
        END) [Has Reader]
FROM 
    dbo.v_R_System_Valid as SV
    JOIN dbo.v_Add_Remove_Programs as ARP
        on SV.ResourceID = ARP.ResourceID
    JOIN dbo.v_GS_OPERATING_SYSTEM as OS
        on SV.ResourceID = OS.ResourceID
WHERE
    OS.Caption0 LIKE '%Windows 10%'
GROUP BY
    SV.Netbios_Name0;