0

我正在尝试创建一个 ConfigMgr 报告,该报告将为我们环境中的每台笔记本电脑显示 3 个注册表项值。我的查询如下所示:

Select distinct

SYS.Netbios_Name0,
(Select REG.Value0 where REG.KeyPath0='HKEY_LOCAL_MACHINE\SOFTWARE\BatteryDesignCapacity' ) as BatteryDesignCapacity,
(Select REG.Value0 where REG.KeyPath0='HKEY_LOCAL_MACHINE\SOFTWARE\BatteryFullChargedCapacity' ) as BatteryFullChargedCapacity,
(Select REG.Value0 where REG.KeyPath0='HKEY_LOCAL_MACHINE\SOFTWARE\BatteryHealth' ) as BatteryHealth

from v_GS_Registry_Values0 REG

INNER JOIN v_R_System_Valid SYS
ON SYS.ResourceID = REG.ResourceID

ORDER BY SYS.Netbios_Name0

但是,当我运行报告时,我会为每个笔记本电脑名称获得 4 行。一行包含笔记本电脑名称和所有空白注册表值,下一行仅包含 BatteryDesignCapacity 值,第三行仅包含 BatteryFullChargedCapacity,第四行仅包含 BatteryHealth。

我究竟做错了什么?是否可以通过修改 select 语句将所有 4 个注册表值放在一行中?

4

2 回答 2

1

我认为你想要条件聚合:

SELECT SYS.Netbios_Name0,
       MAX(CASE WHEN REG.KeyPath0 = 'HKEY_LOCAL_MACHINE\SOFTWARE\BatteryDesignCapacity' THEN REG.Value0  END) as BatteryDesignCapacity,
       MAX(CASE WHEN REG.KeyPath0 = 'HKEY_LOCAL_MACHINE\SOFTWARE\BatteryFullChargedCapacity' THEN REG.Value0 END) as BatteryFullChargedCapacity,
       MAX(CASE WHEN REG.KeyPath0 ='HKEY_LOCAL_MACHINE\SOFTWARE\BatteryHealth' THEN REG.Value0 END) as BatteryHealth
FROM v_GS_Registry_Values0 REG INNER JOIN
     v_R_System_Valid SYS
     ON SYS.ResourceID = REG.ResourceID
GROUP BY SYS.Netbios_Name0
ORDER BY SYS.Netbios_Name0
于 2021-05-04T19:26:50.147 回答
0

看起来 v_GS_Registry_Values0 对于每台计算机都有多行。你可以试试这个,或者使用 SQL PIVOT 函数。

SELECT  DISTINCT
        SYS.Netbios_Name0
        ,MAX(   CASE
                    WHEN REG.KeyPath0 = 'HKEY_LOCAL_MACHINE\SOFTWARE\BatteryDesignCapacity' THEN REG.Value0
                END
            ) AS BatteryDesignCapacity
        ,MAX(   CASE
                    WHEN REG.KeyPath0 = 'HKEY_LOCAL_MACHINE\SOFTWARE\BatteryFullChargedCapacity' THEN REG.Value0
                END
            ) AS BatteryFullChargedCapacity
        ,MAX(   CASE
                    WHEN REG.KeyPath0 = 'HKEY_LOCAL_MACHINE\SOFTWARE\BatteryHealth' THEN REG.Value0
                END
            ) AS BatteryHealth
FROM    v_GS_Registry_Values0 AS REG
        INNER JOIN v_R_System_Valid AS SYS ON SYS.ResourceID = REG.ResourceID
GROUP BY
        SYS.Netbios_Name0;
于 2021-05-04T19:25:25.087 回答