根据年度更新查找每位员工的最新状态

时间:2017-06-23 17:07:33

标签: sql

我的查询有问题。

我有一个名为EMPLOYEE的表,我有字段NAME,YEAR和STATUS。

每位员工都有几年和几个地位,但我需要最近一年的状态。

如何获取该数据?

1 个答案:

答案 0 :(得分:1)

这应该这样做:

SELECT e.[Name],
       e.[Year],
       e.[Status]
FROM employee AS e
     INNER JOIN
(
    SELECT [Name],
           MAX([Year])
    FROM employee
    GROUP BY [Name]
) AS m ON e.[Name] = m.[Name]
          AND e.[Year] = m.[Year];