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