当前位置:网站首页>SQL query duplicate record

SQL query duplicate record

2022-06-26 09:28:00 Great white bear_ BlankBear

1、 Look up redundant duplicate records in the table , Duplicate records are based on a single field (peopleId) To judge
select * from people
where peopleId in (select   peopleId  from   people  group  by   peopleId  having  count(peopleId) > 1)

2、 Delete redundant duplicate records in the table , Duplicate records are based on a single field (peopleId) To judge , There is only rowid Minimum record
delete from people
where peopleId  in (select   peopleId  from people  group  by   peopleId   having  count(peopleId) > 1)
and rowid not in (select min(rowid) from   people  group by peopleId  having count(peopleId )>1)

3、 Look up redundant duplicate records in the table ( Multiple fields )
select * from vitae a
where (a.peopleId,a.seq) in   (select peopleId,seq from vitae group by peopleId,seq  having count(*) > 1)

4、 Delete redundant duplicate records in the table ( Multiple fields ), There is only rowid Minimum record
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、 Look up redundant duplicate records in the table ( Multiple fields ), It doesn't contain rowid Minimum record
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)
( Two )
For example
stay A There is a field in the table “name”,
And between different records “name” The values might be the same ,
Now we need to find out the relationship between the records in the table ,“name” Value has duplicate entries ;
Select Name,Count(*) From A Group By Name Having Count(*) > 1
If the gender is the same, it is as follows :
Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1
( 3、 ... and )
Method 1
declare @max integer,@id integer
declare cur_rows cursor local for select Main field ,count(*) from Table name group by Main field having count(*) >; 1
open cur_rows
fetch cur_rows into @id,@max
while @@fetch_status=0
begin
select @max = @max -1
set rowcount @max
delete from Table name where Main field = @id
fetch cur_rows into @id,@max
end
close cur_rows
set rowcount 0

   Method 2

   There are two meanings of duplicate records , One is a completely repeated record , That is, all the fields are duplicate records , Second, some records with duplicate key fields , such as Name Field repeat , Other fields are not necessarily repeated or can be ignored .

  1、 For the first kind of repetition , It's easier to solve , Use
select distinct * from tableName

   You can get a result set without duplicate records .

   If the table needs to delete duplicate records ( Duplicate record retention 1 strip ), You can delete it as follows
select distinct * into #Tmp from tableName
drop table tableName
select * into tableName from #Tmp
drop table #Tmp

   The reason for this duplication is that the table is not designed properly , Adding a unique index column can solve the problem .

  2、 This kind of duplication problem usually requires that the first record in the duplicate record be kept , The operation method is as follows

   Suppose there are duplicate fields as Name,Address, The only result set of these two fields is required
select identity(int,1,1) as autoID, * into #Tmp from tableName
select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID
select * from #Tmp where autoID in(select autoID from #tmp2)

   the last one select taken Name,Address Result sets that don't repeat ( But one more autoID Field , When actually writing, you can write it in select Clause )

( Four ) Duplicate query
select * from tablename where id in (
select id from tablename
group by id
having count(id) > 1
)
 

原网站

版权声明
本文为[Great white bear_ BlankBear]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170550232464.html