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

标签 sql sql-server tsql duplicates

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

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

它返回 423 行,

我执行了另一个查询来查找不重复的记录

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

返回 685 条记录

I found that there are 196 distinct records among the 423 duplicate Now, How to select the first record from duplicate records?

最佳答案

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

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

关于SQL:如何从重复行中选择第一条记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49474997/

相关文章:

mysql - 将用户表连接到两个不同网站的两个不同数据库

c# - 为什么我的 SQL 'INSERT' 语句执行了两次?

sql-server - t-sql : create and add several namespaces to xml

sql-server - Where 中的条件和 Join 中的条件之间的区别

sql-server - 传递 SQL 函数数据库名称

java - 如何在 SQL 字符串中插入多个 where 子句

PHP 从没有小时/分钟的数据库中获取日期时间?

sql - 在 SQL Server 中,我想将字符串与 '~' 分开

sql - TSQL根据不同的字段计算各种百分比

tsql - 什么时候死锁不是死锁?