在一个表中按最大日期过滤的重复行的SQL查询

时间:2014-05-05 15:18:54

标签: sql sql-server

我有一个sql数据库,“人员”,根据IDNum列有重复的条目。我需要查询条目,只显示基于最新创建日期的行或重复条目。 这是查询:

SELECT IDNum
      ,PersonPGUID
      ,CreatedDateTime
      ,FirstName
      ,MiddleName
      ,LastName
  FROM [Persons]
  WHERE IDNum in (298928, 509520, 528203);

它返回:

IDNum   PersonPGUID                             CreatedDateTime         FirstName   MiddleName  LastName
298928  C43DEB67-EB04-4066-A374-B8369D508CF6    2007-10-22 10:13:45.000 Jonathan    McKinley    Bennett-Tisdell
298928  75B63500-7C67-4B54-A2C1-F74EAF98B861    2007-10-22 10:15:35.000 Jonathan    McKinley    Bennett-Tisdell
509520  57A1FB38-93B5-4D7B-8A17-243EC9330766    2005-02-10 11:55:13.000 Lannon      Caleb       Morgan
509520  176667F4-318E-415B-AFFC-A80348325A9D    2004-08-24 06:26:27.000 Lannon      Caleb       Morgan
528203  36C372CB-C9AD-4CEC-8553-8147C7FEDE20    2009-06-08 09:26:43.000 Pedro       Trigueros
528203  5B71C0D5-10EB-4375-8F80-E8F01381E08A    2011-12-15 10:28:11.000 Pedro       Trigueros

我需要过滤以仅显示重复项,并且它需要是每个重复项的最大日期记录。

谢谢, 威廉

5 个答案:

答案 0 :(得分:3)

试试这个INNER JOIN。它会为您提供良好的性能,因为使用了内联视图。

SELECT [Persons].*
FROM [Persons]
INNER JOIN
(
    SELECT 
          IDNum, 
          MAX(CreatedDateTime) AS CreatedDateTime
      FROM [Persons]
      WHERE IDNum in (298928, 509520, 528203)
      GROUP BY IDNum
      HAVING COUNT(*) > 1
) latest_records
ON [Persons].IDNum = latest_records.IDNum AND [Persons].CreatedDateTime = latest_records.CreatedDateTime;

答案 1 :(得分:1)

您可以将CTE与窗口函数结合使用,以获取每个IDNum的最新记录

WITH CTE
AS
(
  SELECT
       IDNum
      ,PersonPGUID
      ,CreatedDateTime
      ,FirstName
      ,MiddleName
      ,LastName 
      , ROW_NUMBER() OVER(PARTITION BY IDNum ORDER BY CreatedDateTime DESC) RN
  FROM [Persons]
  WHERE IDNum in (298928, 509520, 528203)
)
SELECT IDNum
      ,PersonPGUID
      ,CreatedDateTime
      ,FirstName
      ,MiddleName
      ,LastName
FROM CTE
WHERE RN = 1

答案 2 :(得分:1)

如果CreatedDateTime始终是一个完全唯一的列(即没有两个值重复):

SELECT * FROM PERSONS WHERE CreatedDateTime IN
(SELECT MAX(CreatedDateTime) from Persons group by IDnum having count(IDnum) > 1)

嗨威廉,

感谢您的10分,我只是认为该表还可能包含IdNum值不重复的行。你的问题显示3个重复的IdNum,所以我不知道是否存在重复的IdNum,但我想也许如此。如果是这样,你可以使用它:

SELECT * FROM PERSONS WHERE CreatedDateTime IN (SELECT MAX(CreatedDateTime)
FROM Persons GROUP BY IDnum HAVING COUNT(IDnum) > 1 OR COUNT(IDNUM) = 1)

答案 3 :(得分:0)

这通常使用相关子查询来完成:

SELECT IDNum ,PersonPGUID ,CreatedDateTime ,FirstName ,MiddleName ,LastName 
FROM [Persons] p
WHERE IDNum in (298928, 509520, 528203)
   and CreatedDateTime = (Select max(CreatedDateTime)
                          Where IDNum = p.IDNum)

答案 4 :(得分:0)

这对你有用吗?基本上选择结果的最大时间和连接

select * 
from persons p inner join (
      select max(CreatedDatetime) as maxTime, IDNum as maxId from persons group by IDNum) maxTimeSet
           on maxTimeSet.maxId = p.IDNum