Getting the last nationality for an employee

前端 未结 1 1483
南旧
南旧 2021-01-29 04:43

I am trying to get the list of employees and their nationalities :

 select concat([Firstname],[Lastname]) as \'Full name\',[C].[Label] as \'Nationality\' FROM [         


        
相关标签:
1条回答
  • 2021-01-29 05:32

    Use ROW_NUMBER:

    WITH cte AS (
        SELECT CONCAT([Firstname], [Lastname]) AS full_name,
            [C].[Label] AS Nationality,
            ROW_NUMBER() OVER (PARTITION BY E.EmployeeId ORDER BY UpdateDate DESC) rn
        FROM [Employee] [E]
        LEFT JOIN [AF_AdminFile] [AFA]
            ON E.AdminFileId = AFA.AdminFileId
        LEFT JOIN [AF_Nationality] [AFN]
            ON AFN.AdminFileId = AFA.AdminFileId
        LEFT JOIN [Country] [C]
            ON AFN.CountryId = C.ID
    )
    
    SELECT
        full_name,
        Nationality
    FROM cte
    WHERE rn = 1;
    
    0 讨论(0)
提交回复
热议问题