我正在使用System Center数据来处理更新合规性报告。我有我想要的查询,但我无法删除失败更新或我的必需/已部署更新的计数为0的行。
我玩过HAVING,但只能让它返回NULL,但仍然不能排除它们。
select distinct
rs.name0 as 'Computer Name',
rs.description0 AS 'Description',
os.Caption0 as 'Operating System',
case
when CS.ClientActiveStatus='1' then 'Active'
when CS.ClientActiveStatus='0' then 'Inactive'
end
as 'Client Active Status',
--Last Hardware Scan
convert(varchar(26), ws.lasthwscan, 100) as 'Last Hardware Scan',
--Last Software Scan (Convert to EST)
convert(varchar(26), (dateadd(hour, -4, uss.LastScanTime)), 100) as 'Last Software Scan',
--Required/Deployed Updates
(select count(ucs.ResourceID)
from v_Update_ComplianceStatus ucs
join v_updateinfo ui on ui.ci_id=ucs.ci_id
join V_CITargetedMachines ctm on ctm.ci_id=ucs.ci_id and ctm.ResourceID = ucs.ResourceID
where ucs.resourceid=rs.ResourceID and ucs.Status='2' and ui.LocaleID='9' and ui.IsDeployed='1' and ui.IsSuperseded='0'
) as 'Required/Deployed Updates',
--Failed Updates
(select count(ucs.ResourceID)
from v_UpdateComplianceStatus ucs
join v_UpdateInfo ui on ucs.ci_id=ui.ci_id and ucs.Status='2' and ui.LocaleID='9' and ui.IsDeployed='1' and ui.IsSuperseded='0' and ucs.LastEnforcementMessageID In (6,11,14)
join v_StateNames sn on ucs.LastEnforcementMessageID = sn.StateID and sn.TopicType=402
where ucs.resourceid=rs.ResourceID
) as 'Failed Updates',
--Reboot Check
(select iif(sum(iif(ucs.LastEnforcementMessageID='9',1,0)) > 0,'Yes','No')
from v_UpdateComplianceStatus ucs
where ucs.resourceid=rs.ResourceID
) as 'Reboot Required'
from v_R_System rs
join v_GS_OPERATING_SYSTEM os on rs.ResourceID=os.ResourceID
join v_FullCollectionMembership fcm on rs.ResourceID=fcm.ResourceID
join v_Collection col on fcm.CollectionID=col.CollectionID
Join v_CH_ClientSummary CS on rs.ResourceId=CS.ResourceID
join v_GS_WORKSTATION_STATUS ws on rs.ResourceID=ws.ResourceID
join v_UpdateScanStatus uss on rs.ResourceID=uss.ResourceID
where 'Required/Deployed Updates' > '0' and 'Failed Updates' > '0'
order by rs.Name0 asc
我希望排除“必需/已部署的更新”或“失败的更新”计数为0。
当前正在获取
Computer Name | Description | Operating System | Client Active Status | Last Hardware Scan | Last Software Scan | Required/Deployed Updates | Failed Updates | Reboot Required
WKS1 | Test Laptop 1 | Microsoft Windows 10 Enterprise | Active | Apr 5 2019 12:00PM | Apr 5 2019 12:00PM | 0 | 0 | No
WKS2 | Test Laptop 2 | Microsoft Windows 10 Enterprise | Active | Apr 2 2019 12:46PM | Apr 5 2019 12:02PM | 0 | 0 | Yes
WKS3 | Test Laptop 3 | Microsoft Windows 10 Enterprise | Active | Apr 2 2019 12:46PM | Apr 5 2019 12:02PM | 1 | 1 | Yes
想获得
Computer Name | Description | Operating System | Client Active Status | Last Hardware Scan | Last Software Scan | Required/Deployed Updates | Failed Updates | Reboot Required
WKS3 | Test Laptop 3 | Microsoft Windows 10 Enterprise | Active | Apr 2 2019 12:46PM | Apr 5 2019 12:02PM | 1 | 1 | Yes
任何帮助将不胜感激。
答案 0 :(得分:0)
您不能在WHERE
子句中使用别名。
您可以将另一个SELECT
包裹起来,并在其中的别名表达式上应用条件。
SELECT ...
FROM (SELECT ...,
(SELECT count(...)
FROM v_update_compliancestatus ucs
...) "Required/Deployed Updates"
(SELECT count(...)
FROM v_update_compliancestatus ucs
...) "Failed Updates",
FROM ...
WHERE ...) x
WHERE "Required/Deployed Updates" > 0
AND "Failed Updates" > 0;
或者,您可以简单地重复该表达式,以子选择为例。
SELECT ...
(SELECT count(...)
FROM v_update_compliancestatus ucs
...) "Required/Deployed Updates",
(SELECT count(...)
FROM v_update_compliancestatus ucs
...) "Failed Updates"
...
FROM ...
WHERE ...
AND (SELECT count(...)
FROM v_update_compliancestatus ucs
...) > 0
AND (SELECT count(...)
FROM v_update_compliancestatus ucs
...) > 0
...
在您的情况下,由于要检查表的行数不为零,即如果存在一行,则还可以在EXISTS
子句中使用WHERE
,这可能会加快速度。
SELECT ...
(SELECT count(...)
FROM v_update_compliancestatus ucs
...) "Required/Deployed Updates",
(SELECT count(...)
FROM v_update_compliancestatus ucs
...) "Failed Updates"
...
FROM ...
WHERE ...
AND EXISTS (SELECT *
FROM v_update_compliancestatus ucs
...)
AND EXISTS (SELECT *
FROM
v_update_compliancestatus ucs)
...
还要注意,您不需要将数字文字用单引号('
换行。也不要将别名用单引号换行,如果需要,可以使用双引号("
)。或需要引用别名。在SQL Server中,您还可以使用[alias]
中的方括号。