SQL:如何从重复的行中选择第一条记录?

Bod*_*dhi 1 sql t-sql sql-server duplicates

在执行以下查询以查找重复项时

select * from (
select a.* ,count (*) over (partition by a.ID) as tot
from HREMP a 
) tt
where tt.tot >1
Run Code Online (Sandbox Code Playgroud)

其返回的423行,

我执行了另一个查询以查找非重复记录

  select * from (
select a.* ,count (*) over (partition by a.ID) as tot
from HREMP a 
) tt
where tt.tot =1
Run Code Online (Sandbox Code Playgroud)

返回685条记录

我发现423个重复的记录中共有196个不同的记录,如何从重复的记录中选择第一个记录?

pap*_*zzo 5

select distinct * 
from ( select a.*, count(*) over (partition by a.ID) as tot
       from HREMP a 
     ) tt
where tt.tot > 1
Run Code Online (Sandbox Code Playgroud)

要么

select * 
from ( select a.*
            , count(*)     over (partition by a.ID) as tot
            , row_number() over (partition by a.ID order by 1) as rn
       from HREMP a 
     ) tt
where tt.tot > 1 
and   tt.rn = 1
Run Code Online (Sandbox Code Playgroud)