SQL query duplicate record, delete duplicate record method Daquan

Source: Internet
Author: User
Tags repetition

Find records for all repeating headings:

SELECT *
From T_info A
WHERE (SELECT COUNT (*)
From T_info
WHERE Title = a.title) > 1)
ORDER by Title DESC

One. Find Duplicate Records

1. Find all duplicate records

SELECT * FROM table Where repeating field in (Select repeating field from table Group by repeating field having Count (*) >1)

2. Filter duplicate records (show only one bar)

SELECT * from Hzt Where ID in (the Select Max (ID) from Hzt Group by Title)

Note: The ID maximum one record is shown here

Two. Delete duplicate records


1. Delete all duplicate records (with caution)

Delete table Where repeating field in (Select repeating field from table Group by repeating field having Count (*) >1)

2. Keep One (this should be what most people need ^_^)

Delete hzt Where ID not in (the Select Max (ID) from Hzt Group by Title)

Note: The maximum one record for the ID is retained here

1, look for redundant records in the table, duplicate records are based on a single field (Peopleid) to determine

SELECT * from People

where Peopleid in (select Peopleid from People GROUP by Peopleid have count (Peopleid) > 1)



2, delete redundant records in the table, duplicate records are based on a single field (Peopleid) to judge, leaving only the smallest ROWID records

Delete from people

where Peopleid in (select Peopleid from People GROUP by Peopleid have count (Peopleid) > 1)

and rowID not in (select min (rowid) from people GROUP by Peopleid have Count (Peopleid) >1)



3. Find redundant duplicate records (multiple fields) in the table

SELECT * FROM Vitae a

where (A.PEOPLEID,A.SEQ) in (select Peopleid,seq from Vitae GROUP by PEOPLEID,SEQ have count (*) > 1)



4. Delete extra duplicate records (multiple fields) in the table, leaving only the record with ROWID minimum

Delete from Vitae a

where (A.PEOPLEID,A.SEQ) in (select Peopleid,seq from Vitae GROUP by PEOPLEID,SEQ have count (*) > 1)

and rowID not in (select min (rowid) from Vitae GROUP by PEOPLEID,SEQ have Count (*) >1)



5. Find redundant duplicate records (multiple fields) in the table, not including the smallest ROWID records

SELECT * FROM Vitae a

where (A.PEOPLEID,A.SEQ) in (select Peopleid,seq from Vitae GROUP by PEOPLEID,SEQ have count (*) > 1)

and rowID not in (select min (rowid) from Vitae GROUP by PEOPLEID,SEQ have Count (*) >1)


Add:

There are more than two duplicate records, one is a completely duplicate record, that is, all the fields are duplicated records, and the second is some key field duplicate records, such as the Name field is repeated, and the other fields may not be repeated or repeated can be ignored.


1, for the first kind of repetition, easier to solve, using

SELECT DISTINCT * from TableName


You can get a result set with no duplicate records.



If the table needs to delete duplicate records (duplicate records retain 1), you can delete them as follows

SELECT DISTINCT * to #Tmp from TableName

drop table TableName

SELECT * Into TableName from #Tmp

drop table #Tmp



This duplication occurs because the table is poorly designed and the unique index columns are added to resolve.


2, this kind of repetition problem usually requires to keep the first record in the duplicate record, the operation method is as follows

Suppose there is a duplicate field name,address, which requires the result set to be unique for both fields

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 on (select Autoid from #tmp2)

SQL query duplicate record, delete duplicate record method Daquan

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.