简体   繁体   English

Select 来自同一列的多个值

[英]Select multiple values from the same column

I am trying to write a SCCM query in which I have to pull the installed versions of "MS Office", "MS Visio" and "MS Project" in separate columns.我正在尝试编写一个 SCCM 查询,其中我必须在单独的列中提取已安装的“MS Office”、“MS Visio”和“MS Project”版本。 All the values reside in the same column.所有值都位于同一列中。

The below query is very crude but works.下面的查询非常粗略但有效。 The problem is that it's not aggregated by host name ( CS.Name0 ).问题是它不是按主机名( CS.Name0 )聚合的。 The output for "MS Office", "MS Visio" and "MS Project" are separate rows. “MS Office”、“MS Visio”和“MS Project”的 output 是单独的行。 The requirement is to aggregate it based on the hostname.要求是根据主机名聚合它。 "Group by" did not work. “分组依据”不起作用。

Select DISTINCT
CS.Name0 AS [Computer Name],
SW.ProductName0,
CS.UserName0 as 'User',
DR.LastLogonUser as 'Last Logon User',
DR.PrimaryUser as 'Primary User',
DR.CurrentLogonUser as 'Current Logon User',
Case When (SW.ProductName0 LIKE 'Microsoft Office Standard%' OR SW.ProductName0 LIKE 'Microsoft Office Professional%' OR SW.ProductName0 LIKE 'Microsoft Office Enterprise%'
OR SW.ProductName0 LIKE 'Microsoft Office 365%'
AND SW.ProductName0 NOT LIKE '%MUI%'
AND SW.ProductName0 NOT LIKE '%Proofing%'
AND SW.ProductName0 NOT LIKE '%Herramientas%'
AND SW.ProductName0 NOT LIKE '%components%') Then SW.ProductName0 End As 'Office version',
 
Case When (SW.ProductName0 LIKE '%Microsoft Visio%')  Then Sw.ProductName0 End As 'MS Visio',

Case When (SW.ProductName0 LIKE '%Microsoft Project%')  Then Sw.ProductName0 End As 'MS Project'

FROM v_GS_INSTALLED_SOFTWARE SW
INNER JOIN  v_GS_COMPUTER_SYSTEM CS  ON SW.ResourceID = CS.ResourceID
RIGHT JOIN v_CombinedDeviceResources DR ON DR.MachineID=CS.ResourceID

WHERE SW.ProductName0 LIKE 'Microsoft Office Standard%'
OR SW.ProductName0 LIKE 'Microsoft Office Professional%'
OR SW.ProductName0 LIKE 'Microsoft Office Enterprise%'
OR SW.ProductName0 LIKE 'Microsoft Office 365%'
AND SW.ProductName0 NOT LIKE '%MUI%'
AND SW.ProductName0 NOT LIKE '%Proofing%'
AND SW.ProductName0 NOT LIKE '%Herramientas%'
AND SW.ProductName0 NOT LIKE '%components%'
OR SW.ProductName0 LIKE '%Microsoft Visio%'
OR SW.ProductName0 LIKE '%Microsoft Project%'
AND SW.ProductName0 <> ''


ORDER BY
CS.Name0

The output I am getting is我得到的 output 是

Computer_Name   User    Last_Logon_User Primary_User    Current_Logon_User  Office_version  MS_Visio    MS_Project

hostname1   user    user1   user2   user3   Microsoft Office 365 ProPlus
        
hostname1   user    user1   user2   user3   Microsoft Visio Professional 2016

hostname1   user    user1   user2   user3   Microsoft Project Professional 2016 
    
hostname2   userA   user1A  user2A  user3A  Microsoft Office 365 ProPlus        

hostname2   userA   user1A  user2A  user3A  Microsoft Visio Professional 2016

The desired output is所需的 output 是

Computer_Name   User    Last_Logon_User Primary_User    Current_Logon_User  Office_version  MS_Visio    MS_Project

hostname1   user    user1   user2   user3   Microsoft Office 365 ProPlus  Microsoft Visio Professional 2016 Microsoft Project Professional 2016 

hostname2   userA   user1A  user2A  user3A  Microsoft Office 365 ProPlus Microsoft Visio Professional 2016

I don't know if my query is correct.我不知道我的查询是否正确。 It might need to be changed entirely.它可能需要完全改变。 I have tried many things, nothing worked.我尝试了很多东西,没有任何效果。 Some posts suggested using INNER JOIN for fetching multiple values from the same column, but I could not make that work.一些帖子建议使用INNER JOIN从同一列中获取多个值,但我无法做到这一点。 I am not a developer, but a security guy.我不是开发人员,而是安全人员。 SCCM is a tool which has infrastructure inventory. SCCM 是一种具有基础设施清单的工具。

If you want one and only one result for each of the Microsoft Office products per computer_name , proceed as follows:如果您希望每个computer_name的每个 Microsoft Office 产品只有一个结果,请执行以下操作:

with current_results as (
CS.Name0 AS Computer_Name,
CS.UserName0 as UserID,
DR.LastLogonUser as Last_Logon_User,
DR.PrimaryUser as Primary_User,
DR.CurrentLogonUser as Current_Logon_User,
SW.ProductName0 office_software
FROM v_GS_INSTALLED_SOFTWARE SW
INNER JOIN  v_GS_COMPUTER_SYSTEM CS  ON SW.ResourceID = CS.ResourceID
RIGHT JOIN v_CombinedDeviceResources DR ON DR.MachineID=CS.ResourceID
WHERE SW.ProductName0 LIKE 'Microsoft Office Standard%'
OR SW.ProductName0 LIKE 'Microsoft Office Professional%'
OR SW.ProductName0 LIKE 'Microsoft Office Enterprise%'
OR SW.ProductName0 LIKE 'Microsoft Office 365%'
AND SW.ProductName0 NOT LIKE '%MUI%'
AND SW.ProductName0 NOT LIKE '%Proofing%'
AND SW.ProductName0 NOT LIKE '%Herramientas%'
AND SW.ProductName0 NOT LIKE '%components%'
OR SW.ProductName0 LIKE '%Microsoft Visio%'
OR SW.ProductName0 LIKE '%Microsoft Project%'
AND SW.ProductName0 <> ''
)
select distinct Computer_Name, UserID, Last_Logon_User, Primary_User, Current_Logon_User,
max(case when office_software like '%Office%' then office_software end) over(partition by computer_name) Office_version,  
max(case when office_software like '%Visio%' then office_software end)  over(partition by computer_name) MS_Visio,  
max(case when office_software like '%Project%' then office_software end)  over(partition by computer_name) MS_Project  
from current_results

If you are looking for the office product by the entire row of columns, you can run this simpler version (with the current_results CTE as defined previously):如果您正在按整行列查找办公产品,您可以运行这个更简单的版本(使用前面定义的current_results CTE ):

select Computer_Name, UserID, Last_Logon_User, Primary_User, Current_Logon_User,
max(case when office_version like '%Office%' then office_version end)  Office_version,  
max(case when office_version like '%Visio%' then office_version end)   MS_Visio,  
max(case when office_version like '%Project%' then office_version end)   MS_Project  
from current_results
group by 1,2,3,4,5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM