如何根据条件选择不同的记录

时间:2014-10-21 10:37:03

标签: sql sql-server duplicate-removal

我有enter image description here等重复记录表 现在我只想要一个记录来自重复记录,其最新创建日期为enter image description here]![enter image description here我该怎么做?

2 个答案:

答案 0 :(得分:3)

使用row_number()

select EnquiryId, Name, . . .
from (select t.*,
             row_number() over (partition by enquiryID order by CreatedDate desc) as seqnum
      from table t
     ) t
where seqnum = 1;

答案 1 :(得分:3)

使用ROW_NUMBER函数标记CreatedDate排序的重复记录,如下所示:

;with CTE AS (
    select *, row_NUMBER() over(
                 partition by EnquiryID -- add columns on which you want to identify duplicates
                 ORDER BY CreatedDate DESC) as rn
    FROM TABLE 
)
select * from CTE
where rn = 1