How to select distinct records based on condition

2019-09-22 08:09发布

I have table of duplicate records like enter image description here Now I want only one record from duplicate records which has latest created date as enter image description here]![enter image description hereHow can I do it ?

2条回答
劳资没心,怎么记你
2楼-- · 2019-09-22 08:47

Use ROW_NUMBER function to tag the duplicate records ordered by CreatedDate, like this:

;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
查看更多
仙女界的扛把子
3楼-- · 2019-09-22 08:54

use 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;
查看更多
登录 后发表回答