Summarize SQL queries duplicate records and Delete methods "the teacher says it's worth collecting."

Source: Internet
Author: User
Tags min repetition

Find records for all duplicate headings:

SELECT * from T_info a WHERE (SELECT COUNT (*) from t_info where title = A.title) > 1) Order by Title DESC

First, find duplicate records

1. Find all duplicate records

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

2. Filter duplicate record (show only one)

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

Note: A record with the largest ID is shown here

Ii. deletion of duplicate records

1. Delete all duplicate records (use caution)

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

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

Delete hzt Where ID. (Select Max (ID) from Hzt Group by Title)

Note: A record with the largest ID is reserved here

Iii. examples

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

SELECT * from people where Peopleid to (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 rowid minimal records

Delete from people where Peopleid on (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 records in the table (multiple fields)

SELECT * from Vitae a WHERE (A.PEOPLEID,A.SEQ) in (select Peopleid,seq to Vitae GROUP by PEOPLEID,SEQ have Count (*) &G T 1)

4, delete redundant records in the table (multiple fields), leaving only the smallest ROWID records

Delete from Vitae a where (A.PEOPLEID,A.SEQ) in (select Peopleid,seq to 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, look for redundant records in the table (multiple fields), does not contain the smallest ROWID records

SELECT * from Vitae a WHERE (A.PEOPLEID,A.SEQ) in (select Peopleid,seq to Vitae GROUP by PEOPLEID,SEQ have Count (*) &G T 1) and rowID (select min (rowid) from Vitae GROUP by PEOPLEID,SEQ have Count (*) >1)

Iv. Supplementary

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

1, for the first repetition, easier to solve, the use of

SELECT DISTINCT * FROM tablename

You can get a result set without duplicate records.

If the table needs to delete duplicate records (1 of duplicate records are retained), you can delete them in the following ways

SELECT DISTINCT * into #Tmp tablename
DROP TABLE TableName
SELECT * INTO TableName from #Tmp
drop table #Tmp

This duplication occurs because the table is poorly designed and can be resolved by adding a unique index column.

2. This type of repetition usually requires the first record in the duplicate record to be retained, as follows

Suppose there is a duplicate field of name,address that requires a unique result set for both fields


Select Identity (int,1,1) as Autoid, * into #Tmp to tablename 
Select min (autoid) as autoid into #Tmp2 From #Tmp GROUP BY name,autoid 
Select * from #Tmp where autoid in (select Autoid from #tmp2)

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.