• How about:

    WITH cteUsersAndSoftware(UserName, Software)

    AS

    (

    SELECT DISTINCT s.User_Domain0 + '\' + s.User_Name0 AS 'UserName',

    (CASE

    WHEN p.DisplayName0 LIKE 'Microsoft Visual Studio% %Ultimate% %2013%' OR ... THEN 'Microsoft Visual Studio Ultimate 2013'

    WHEN p.DisplayName0 LIKE 'Microsoft Visual Studio% %Ultimate% %2012%' OR ... THEN 'Microsoft Visual Studio Ultimate 2012'

    .

    .

    WHEN p.DisplayName0 LIKE 'Microsoft Visual Studio% %Professional% %2005%' OR ... THEN 'Microsoft Visual Studio Professional 2005'

    END) AS 'Software'

    FROM dbo.v_Add_Remove_Programs p

    INNER JOIN dbo.v_R_System s ON p.ResourceID = s.ResourceID

    ) , cteRank as (

    SELECT cte.UserName

    , rank() over (partition by cte.UserName order by rnk.vsVersion) as ProductRank

    , rnk.vsVersion

    FROM cteUsersAndSoftware cte

    INNER JOIN #swRank

    rnk ON cte1.Software = rnk.vsVersion

    )

    select * from cte

    where ProductRank = 1

    Russel Loski, MCSE Business Intelligence, Data Platform