关于SQL删除重复数据只保留一条

时间:2022-09-22 22:37:23
用SQL语句,删除掉重复项只保留一条

在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select * from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)

2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
delete from people
where   peopleName in (select peopleName    from people group by peopleName      having count(peopleName) > 1)
and   peopleId not in (select min(peopleId) from people group by peopleName     having count(peopleName)>1)

3、删除表中多余的重复记录,重复记录是根据对个字段来判断的。并且重复条数多余2条。

while 1=1 
begin
    delete top (1) from CNLaw_Info
    from CNLaw_Info p1 join ( select app_no,noticeDate,law_state,law_info from CNLaw_Info group by app_no,noticeDate,law_state
    ,law_info having COUNT(*)>1 )
     p2 on p1.app_no=p2.app_no and p1.noticeDate=p2.noticeDate and p1.law_state=p2.law_state and p1.law_info=p2.law_info
     if @@ROWCOUNT=0 break;
end;


上面这种方法效率非常的低,后来通过改进,例子如下:

select  * from Patent.dbo.patent_US_Grant where ID in(select max(ID)
   from Patent.dbo.patent_US_Grant where NoticeDate='20121023' group by ApplicationNumber having COUNT(ApplicationNumber)>1)

 

如果要删除的话把 “select *” 改为delete

解析上面语句:其实在sql server 最好用上 exists关键字,在这里的后面的select max(ID)。。。 ,查询出id最大的记录。

如果我们在查询时候,不用到having count 那么多个字段相同,找出重复数据的sql:

 比如现在有一人员表  (表名:peosons)
若想将姓名、身份证号、住址这三个字段完全相同的记录查询出来
:select p1.*  from persons  p1,persons p2 where p1.id<>p2.id and p1.cardid = p2.cardid  and p1.pname   = p2.pname and p1.address = p2.address


3、查找表中多余的重复记录(多个字段)
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)   

6.消除一个字段的左边的第一位:

update tableName set [Title]=Right([Title],(len([Title])-1)) where Title like '村%'

7.消除一个字段的右边的第一位:

update tableName set [Title]=left([Title],(len([Title])-1)) where Title like '%村'

8.假删除表中多余的重复记录(多个字段),不包含rowid最小的记录
update vitae set ispass=-1
where peopleId in (select peopleId from vitae group by peopleId