简体   繁体   中英

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. 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 ). The output for "MS Office", "MS Visio" and "MS Project" are separate rows. 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

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

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. I am not a developer, but a security guy. SCCM is a tool which has infrastructure inventory.

If you want one and only one result for each of the Microsoft Office products per computer_name , proceed as follows:

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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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