1 , duplicate records in the lookup table are judged by a single field (Peopleid).
SELECT * from people
where Peopleid in (select Peopleid from People GROUP by Peopleid have count (Peopleid) > 1)
Example Two: Select * from TestTable where numeber in (theselect number from the People Group by number has a count (number) > 1)you can find the same number of records in the TestTable table.
2, delete redundant records in the table, duplicate records are judged by a single field (Peopleid), leaving only the smallest ROWID records
Delete fromPeople
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, finding extra duplicate records in a table (multiple fields)
SELECT * fromVitae 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 fromVitae 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, and do not contain ROWID minimum records
SELECT * fromVitae 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)
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 (*) from A Group by Name have Count (*) > 1
If you also look at the same gender, the following is true:
Select Name,sex,count (*) from A Group by Name,sex have Count (*) > 1
Three
Method One
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
Opencur_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
Closecur_rows
SET ROWCOUNT 0
Method Two
There are two meanings of duplicate records, one is a completely duplicate record, that is, all fields are duplicated records, and the second is some key field duplicate records, such as the Name field repeats, and other fields may not be repeated or can be ignored.
1, for the first repetition, easier to solve, use
SELECT DISTINCT * fromTableName
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 #Tmp fromTableName
drop tableTableName
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 type of repetition usually requires the first record in the duplicate record to be retained, 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 fromTableName
Select min (autoid) as autoid into #Tmp2 from #Tmp group byname,autoid
SELECT * from #Tmp where autoid in (select Autoid 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)
(iv)
Duplicate query
SELECT * FROM tablename where ID in (
Select ID fromTableName
Group byID
Having count (ID) > 1
)
SQL Duplicate record query (reprint)