sccm 查询交叉应用 outerapply 不提供输出

Posted

技术标签:

【中文标题】sccm 查询交叉应用 outerapply 不提供输出【英文标题】:sccm query cross apply outerapply doesnt give output 【发布时间】:2019-08-08 08:38:28 【问题描述】:

我正在运行具有交叉应用和外部应用的 SQL 查询,我需要状态名称为投诉、非投诉和挂起的系统重启,但我获取的数据是针对所有分配 id 的,请查找查询

SELECT    cia.Assignment_UniqueID
        , cia.AssignmentID
        , cia.AssignmentName
        , cia.EnforcementDeadline
        , cia.StartTime
        , sn.StateName
        , NumberOfComputers = sc.StateCount
        , Tot.TotalClientPerAssignment
        , DeploymentStateID=sc.StateType*10000 + sc.StateID
        , PComputers = cast(sc.StateCount * 100.00 / isnull(NULLIF (Tot.TotalClientPerAssignment, 0), 1) AS decimal(5, 2))
FROM v_CIAssignment cia 
CROSS apply
    (SELECT  StateType, StateID, StateCount = count(*)
        FROM  v_AssignmentState_Combined
        WHERE AssignmentID = cia.AssignmentID AND StateType IN (300, 301)
        GROUP BY StateType, StateID) sc 
CROSS apply                                                          
    (SELECT DISTINCT TotalClientPerAssignment = count(atm.ResourceID) OVER (partition BY atm.assignmentid)
        FROM v_CIAssignmentTargetedMachines atm
        WHERE   atm.AssignmentID = cia.AssignmentID) Tot 
LEFT JOIN v_StateNames sn ON sn.TopicType = sc.StateType AND sn.StateID = sc.StateID
WHERE cia.AssignmentID = 2238
 -- and sn.statename like '%Compliant%' or sn.statename like '%Pending system restart%'
    and sn.statename like '%Compliant%' and  sn.statename like '%restart%'
ORDER BY cia.AssignmentID, sc.StateCount DESC, sn.StateName

得到的输出如下

我只需要 statename 作为投诉,非投诉和等待重新启动分配 id 2238,但我正在获取所有分配 id 的数据

你能帮忙吗

查询的输出

【问题讨论】:

轻松为您提供帮助:minimal reproducible example 【参考方案1】:

这是因为您的左连接没有任何 assignmentID

LEFT JOIN v_StateNames sn ON sn.TopicType = sc.StateType AND sn.StateID = sc.StateID

我建议使用以下查询。

SELECT    cia.Assignment_UniqueID
        , cia.AssignmentID
        , cia.AssignmentName
        , cia.EnforcementDeadline
        , cia.StartTime
        , (SELECT StateName FROM v_StateNames WHERE TopicType = sc.StateType AND StateID = sc.StateID and statename like '%Compliant%' and  statename like '%restart%') as StateName
        , NumberOfComputers = sc.StateCount
        , Tot.TotalClientPerAssignment
        , DeploymentStateID=sc.StateType*10000 + sc.StateID
        , PComputers = cast(sc.StateCount * 100.00 / isnull(NULLIF (Tot.TotalClientPerAssignment, 0), 1) AS decimal(5, 2))
FROM v_CIAssignment cia 
CROSS apply
    (SELECT  StateType, StateID, StateCount = count(*)
        FROM  v_AssignmentState_Combined
        WHERE AssignmentID = cia.AssignmentID AND StateType IN (300, 301)
        GROUP BY StateType, StateID) sc 
CROSS apply                                                          
    (SELECT DISTINCT TotalClientPerAssignment = count(atm.ResourceID) OVER (partition BY atm.assignmentid)
        FROM v_CIAssignmentTargetedMachines atm
        WHERE   atm.AssignmentID = cia.AssignmentID) Tot 
WHERE cia.AssignmentID = 2238 
ORDER BY cia.AssignmentID, sc.StateCount DESC

【讨论】:

它给我的 statename 为 null,请找到我需要 statename 作为 3 投诉非投诉和等待系统重启的输出快照

以上是关于sccm 查询交叉应用 outerapply 不提供输出的主要内容,如果未能解决你的问题,请参考以下文章

使用 CROSS APPLY 与 OUTER APPLY 连接查询

利用SQL语句查询SCCM常用报表

lduan SCCM 2012 查询和报表

sccm系统更新补丁后服务无法正常启动

SCCM:在OSD/WinPE中查询域信息的方案

使用多个 OUTER APPLY 优化查询