SQL Remove Duplicate statements

Source: Internet
Author: User
Tags repetition rowcount

SQL single table/multi-table query removes duplicate records

Single table Distinct

Multi-table GROUP by

Group by must be placed before order by and limit, or it will be an error

************************************************************************************

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

Select *  from  where in (select  peopleid from    people  Group by    peopleid have    count>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  fromPeoplewherePeopleidinch(SelectPeopleid fromPeopleGroup   byPeopleid having  Count(Peopleid)> 1) androwID not inch(Select min(ROWID) fromPeopleGroup  byPeopleid having Count(Peopleid)>1)

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

Select *  from where inch  (Selectfromgroup by Peopleid,seq   having Count (*>1)

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

Delete  fromVitae Awhere(A.PEOPLEID,A.SEQ)inch(SelectPeopleid,seq fromVitaeGroup  byPeopleid,seq having Count(*)> 1) androwID not inch(Select min(ROWID) fromVitaeGroup  byPeopleid,seq having Count(*)>1)


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

Select *  fromVitae Awhere(A.PEOPLEID,A.SEQ)inch(SelectPeopleid,seq fromVitaeGroup  byPeopleid,seq having Count(*)> 1) androwID not inch(Select min(ROWID) fromVitaeGroup  byPeopleid,seq having Count(*)>1)

Two
Say
A field "name" exists in table A,
and the "name" value may be the same between different records,
Now is the need to query out the records in the table, "name" value has duplicate entries;

Select Name,Count(*fromGroupby hasCount(* > 1

If you also look at the same gender, the following is true:

Select Name,sex,Count(*fromGroupby hasCount(*  >1

Three
Method One

Declare @max integer,@id integerDeclareCur_rowscursorLocal for SelectMain field,Count(*) fromTable nameGroup  byMain field having Count(*)>;1Opencur_rowsFetchCur_rows into @id,@max while @ @fetch_status=0beginSelect @max = @max -1Set RowCount @maxDelete  fromTable namewhereMain field= @idFetchCur_rows into @id,@maxEndClosecur_rowsSet RowCount 0

Method Two

Duplicate records have two meanings of duplicate records, one is a completely duplicate record, that is, all fields are duplicated records, second, some key fields 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 *  into  from TableName Drop Table TableName Select *  into  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) asAutoid,*  into#Tmp fromTableNameSelect min(autoid) asAutoid into#Tmp2 from#TmpGroup  byname,autoidSelect *  from#TmpwhereAutoidinch(SelectAutoid from#tmp2)
The last select is the result set that name,address not duplicate (but one more autoid field, which can be written in the SELECT clause without this column in the actual write)

Four
Duplicate query

Select *  from where inch (Selectfromgroupby hascount>  1 )

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

Select *  from where inch (selectfromgroupby havecount(*>  1)

Run will cause problems, where (A.PEOPLEID,A.SEQ) such a write hair is not pass!!!

SQL Remove Duplicate statements

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.