1

有一个结果集,其中所有记录都是这样的唯一列。如果有另一条属于同一 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
4

4 回答 4

1

您需要一个子选择来查找每个BusinessEntityID. 试试这个(当然未经测试):

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 
   WHERE x.StartDate = 
     (SELECT 
        Max(cs.StartDate) 
      FROM
        CS120Exam_EmployeeDepartmentHistory cs
      WHERE
        cs.BusinessEntityID = x.BusinessEntityID)
   ORDER BY BusinessEntityID
于 2012-12-09T05:53:54.957 回答
0

您可以使用 GROUP BY 和 max(startdate)

SELECT 
  FirstName,
  LastName,
  Department,
  max(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
  GROUP BY 
    FirstName,
    LastName,
    Department,
    Title,
    PhoneNumber,
    BusinessEntityID
于 2012-12-09T05:37:28.650 回答
0

我正在考虑按其余列分组并获得最大日期。在这种情况下,您将获得一条具有最大日期的记录

SELECT FirstName,LastName,Department,max(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 
group by  FirstName,LastName,Department,Title,PhoneNumber,BusinessEntityID
ORDER BY BusinessEntityID
于 2012-12-09T05:38:31.257 回答
0
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,
     row_number() over (partition by BusinessID order by StartDate desc) as rn
   FROM 
     CS120Exam_EmployeeDepartmentHistory H
   JOIN 
     CS120Exam_Employee E
   ON 
     E.BusinessEntityID=H.BusinessEntityID ) x 
WHERE rn = 1
ORDER BY BusinessEntityID

可能比使用子选择获得最大值更快。startdate,因为只需要对表进行一次扫描。

于 2012-12-09T07:53:32.747 回答