Search code examples
sqlsccm

Query in SQL (SCCM Database) that selects only the newest record when there is a duplicate


I need to modify the following query to select only the records with the newest LastLogin if the BIOSSerialNumber is duplicated. I have tried the solutions proposed in similar questions (row_number(), subqueries,...), but I'm not able to make it work.

It's a SQL query to the SCCM database:

SELECT 
v_GS_COMPUTER_SYSTEM.Name0 as name,
v_GS_COMPUTER_SYSTEM_PRODUCT.Version0 as model,
v_GS_COMPUTER_SYSTEM.ResourceID,
v_GS_SYSTEM.SystemRole0 as SystemRole,
v_GS_OPERATING_SYSTEM.Caption0 as caption,
v_GS_SYSTEM_ENCLOSURE.ChassisTypes0 as chassistype,
v_GS_WORKSTATION_STATUS.LastHWScan, 
v_GS_PC_BIOS.SerialNumber0 as BIOSSerialNumber,
v_GS_COMPUTER_SYSTEM_PRODUCT.UUID0 as UUIDSerialNumber,
v_GS_SYSTEM_ENCLOSURE.SerialNumber0 as ChassisSerialNumber,
v_GS_BASEBOARD.SerialNumber0 as BaseboardSerialNumber,
v_R_SYSTEM.Last_Logon_Timestamp0 as LastLogin

FROM v_GS_COMPUTER_SYSTEM
LEFT JOIN v_GS_WORKSTATION_STATUS ON v_GS_COMPUTER_SYSTEM.ResourceID = v_GS_WORKSTATION_STATUS.ResourceID 
LEFT JOIN v_GS_SYSTEM ON v_GS_COMPUTER_SYSTEM.ResourceID = v_GS_SYSTEM.ResourceID 
LEFT JOIN v_GS_PC_BIOS ON v_GS_COMPUTER_SYSTEM.ResourceID = v_GS_PC_BIOS.ResourceID
LEFT JOIN v_GS_OPERATING_SYSTEM ON v_GS_COMPUTER_SYSTEM.ResourceID = v_GS_OPERATING_SYSTEM.ResourceID
LEFT JOIN v_GS_COMPUTER_SYSTEM_PRODUCT ON v_GS_COMPUTER_SYSTEM.ResourceID = v_GS_COMPUTER_SYSTEM_PRODUCT.ResourceID
LEFT JOIN v_GS_SYSTEM_ENCLOSURE ON v_GS_COMPUTER_SYSTEM.ResourceID = v_GS_SYSTEM_ENCLOSURE.ResourceID
LEFT JOIN v_GS_BASEBOARD ON v_GS_COMPUTER_SYSTEM.ResourceID = v_GS_BASEBOARD.ResourceID
LEFT JOIN v_R_SYSTEM ON v_GS_COMPUTER_SYSTEM.ResourceID = v_R_SYSTEM.ResourceID

If the result is:

name    model   ResourceID  …   BIOSSerialNumber    …   LastLogin
ABC-123 HP XYZ  567845678   …   SerialNumber1       …   2020-10-26 21:14:28.000
DEF-456 HP XYZ  984567432   …   SerialNumber1       …   2020-10-24 07:32:05.000
EFG-789 HP XYZ  127687643   …   SerialNumber2       …   2020-10-21 08:35:05.000

I need to return only rows 1 and 3 because row 2 has same BIOSSerialNumber as 1 but the LastLogin is older:

name    model   ResourceID  …   BIOSSerialNumber    …   LastLogin
ABC-123 HP XYZ  567845678   …   SerialNumber1       …   2020-10-26 21:14:28.000
EFG-789 HP XYZ  127687643   …   SerialNumber2       …   2020-10-21 08:35:05.000

Is there any way to acheive that? Many thanks in advance.


Solution

  • Using the row_number function, if you partition by BIOSSerialNumber and order by LastLogin like this:

    ;with cte_SCCM as
    (
        SELECT 
        v_GS_COMPUTER_SYSTEM.Name0 as name,
        v_GS_COMPUTER_SYSTEM_PRODUCT.Version0 as model,
        v_GS_COMPUTER_SYSTEM.ResourceID,
        v_GS_SYSTEM.SystemRole0 as SystemRole,
        v_GS_OPERATING_SYSTEM.Caption0 as caption,
        v_GS_SYSTEM_ENCLOSURE.ChassisTypes0 as chassistype,
        v_GS_WORKSTATION_STATUS.LastHWScan, 
        v_GS_PC_BIOS.SerialNumber0 as BIOSSerialNumber,
        v_GS_COMPUTER_SYSTEM_PRODUCT.UUID0 as UUIDSerialNumber,
        v_GS_SYSTEM_ENCLOSURE.SerialNumber0 as ChassisSerialNumber,
        v_GS_BASEBOARD.SerialNumber0 as BaseboardSerialNumber,
        v_R_SYSTEM.Last_Logon_Timestamp0 as LastLogin,
        ROW_NUMBER() OVER (PARTITION BY v_GS_PC_BIOS.SerialNumber0 ORDER BY v_R_SYSTEM.Last_Logon_Timestamp0 DESC) as RN
    
        FROM v_GS_COMPUTER_SYSTEM
        LEFT JOIN v_GS_WORKSTATION_STATUS ON v_GS_COMPUTER_SYSTEM.ResourceID = v_GS_WORKSTATION_STATUS.ResourceID 
        LEFT JOIN v_GS_SYSTEM ON v_GS_COMPUTER_SYSTEM.ResourceID = v_GS_SYSTEM.ResourceID 
        LEFT JOIN v_GS_PC_BIOS ON v_GS_COMPUTER_SYSTEM.ResourceID = v_GS_PC_BIOS.ResourceID
        LEFT JOIN v_GS_OPERATING_SYSTEM ON v_GS_COMPUTER_SYSTEM.ResourceID = v_GS_OPERATING_SYSTEM.ResourceID
        LEFT JOIN v_GS_COMPUTER_SYSTEM_PRODUCT ON v_GS_COMPUTER_SYSTEM.ResourceID = v_GS_COMPUTER_SYSTEM_PRODUCT.ResourceID
        LEFT JOIN v_GS_SYSTEM_ENCLOSURE ON v_GS_COMPUTER_SYSTEM.ResourceID = v_GS_SYSTEM_ENCLOSURE.ResourceID
        LEFT JOIN v_GS_BASEBOARD ON v_GS_COMPUTER_SYSTEM.ResourceID = v_GS_BASEBOARD.ResourceID
        LEFT JOIN v_R_SYSTEM ON v_GS_COMPUTER_SYSTEM.ResourceID = v_R_SYSTEM.ResourceID
    )
    
    SELECT *
    FROM cte_SCCM 
    WHERE RN = 1