有一个结果集,其中所有记录都是这样的唯一列。如果有另一条属于同一 BusinessID 的记录,我只想显示具有较新日期的记录。我得到这个结果的查询是
SELECT
FirstName,
LastName,
Department,
StartDate,
Title,
PhoneNumber,
BusinessEntityID
FROM
(SELECT
E.BusinessEntityID,
H.FirstName,
H.LastName,
H.Title,
H.Department,
E.PhoneNumber,
E.CountryRegionName,
E.PostalCode,
H.StartDate
FROM
CS120Exam_EmployeeDepartmentHistory H
JOIN
CS120Exam_Employee E
ON
E.BusinessEntityID=H.BusinessEntityID ) x
ORDER BY BusinessEntityID
结果是
BusinessID FirstName LastName Department StartDate
----------- --------- -------- ---------- ----------
1 aaa mate staff 2002-02-02 <----- DO NOT want this
1 aaa mate admin 2004-03-05
2 john mate admin 2001-03-06
3 sun kent admin 2004-03-05
4 bbb clark staff 2006-02-02 <----- DO NOT want this
4 bbb clark admin 2009-03-05