来自sql union的项目百分比

时间:2016-08-25 15:38:08

标签: sql reporting-services sccm

我正在尝试从SCCM数据库中的两个表的并集返回一个名为“Percent%”的列。我试图返回的数据是版本的百分比,我不完全确定如何编写代码。这就是我所拥有的。

DECLARE @Collection varchar(10)
SET @Collection = 'CM1000XX'

SELECT  [Software Installed], 
        Publisher, 
        Version, 
        COUNT(*) AS 'Client Count'
        --,(STR((COUNT(v_R_System.ResourceID)*100.0/(SELECT COUNT(Add_Remove_Programs_DATA.ResourceID))))) AS 'Percent %'
FROM(
    SELECT      Add_Remove_Programs_DATA.displayname00  as 'Software Installed', 
            Add_Remove_Programs_DATA.publisher00    as 'Publisher', 
            Add_Remove_Programs_DATA.version00      as 'Version'            
FROM        v_R_System AS Sys
LEFT JOIN   v_fullcollectionmembership FCM
ON          FCM.resourceid = Sys.resourceid
INNER JOIN  Add_Remove_Programs_DATA 
ON          Sys.ResourceID = Add_Remove_Programs_DATA.MachineID 
WHERE       FCM.collectionid = @Collection      
AND         (Add_Remove_Programs_DATA.Displayname00 = 'Microsoft Office 365 ProPlus - en-us'
OR          Add_Remove_Programs_DATA.Displayname00 LIKE 'Microsoft Office Professional%')
AND         Add_Remove_Programs_DATA.Version00 LIKE '1%'

UNION ALL

SELECT      Add_Remove_Programs_64_DATA.displayname00   as 'Software Installed', 
            Add_Remove_Programs_64_DATA.publisher00     as 'Publisher',
            Add_Remove_Programs_64_DATA.version00       as 'Version'
FROM        v_R_System AS Sys
LEFT JOIN   v_fullcollectionmembership FCM
            ON FCM.resourceid = Sys.resourceid
INNER JOIN  Add_Remove_Programs_64_DATA 
ON          Sys.ResourceID = Add_Remove_Programs_64_DATA.MachineID 
WHERE       FCM.collectionid = @Collection      
AND         (Add_Remove_Programs_64_DATA.Displayname00 = 'Microsoft Office 365 ProPlus - en-us'
OR          Add_Remove_Programs_64_DATA.Displayname00 LIKE 'Microsoft Office Professional%')
AND         Add_Remove_Programs_64_DATA.Version00 LIKE '1%') AS x64
group by Version, [Software Installed], Publisher
order by Version

非常感谢任何帮助或指向正确的方向。

2 个答案:

答案 0 :(得分:0)

首先需要更正UNION中第二个SELECT语句的列数,因为UNION中使用的两个语句必须具有相同的列数。

其次,要获得UNION的百分比,您必须执行另一个SELECT语句,其中FROM的表将是UNION的结果。

然后你需要这个

Select *, 
(
select s.'Client Count'*100 / (select sum('Client Count') from s)
) as 'Percent %'  
from (your query before) as s

答案 1 :(得分:0)

我想我在数据库中找到了一个视图,它为我生成了一个删除复杂性v_Add_Remove_Programs的联合。我会去那里工作,谢谢你的帮助!