SQL Duplicate record query
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)
Case Two:
SELECT * from TestTable
where Numeber to (select number from people group by number has count (number) > 1)
can detect the same number of records in the TestTable table
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 (select min (rowid) from people GROUP by Peopleid has 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 has count (* ) > 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 has count (* ) > 1) and rowID not in
(select min (rowid) from Vitae GROUP by PEOPLEID,SEQ have Count (*) >1)
Two
Say
There is a field "name" in Table A,
and the "name" value may be the same between different records,
Now you need to query the records in the table, the "name" value duplicates the item;
Select Name,count (*) from-A Group by Name has Count (*) > 1
If the same gender is also the same large as the following:
Select Name,sex,count (*) from-A Group by Name,sex have Count (*) > 1
Three
Method One
Declare @max integer, @id the integer
declare cur_rows cursor The local for select main field, the Count (*) from table name GROUP by main field has Cou NT (*) >; 1
open cur_rows
fetch cur_rows into @id, @max the 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 close
cur_ Rows
Method Two
There are two duplicates of the record, 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 repeat, 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 from tablename
drop TABLE tablename
SELECT * to 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 from tablename
Select min (autoid) as autoid to #Tmp2 from #Tmp Group by Name,autoid
SELECT * to #Tmp where autoid in (select Autoid from #tmp2)
The last select gets the name,address result set (but one more autoid field, which can be written in the SELECT clause to omit this column)
Four
Query duplication
SELECT * FROM tablename where ID in (
select ID from tablename
GROUP by ID has
count (id) > 1
The above is a small series for everyone to bring the SQL duplicate records query query multiple fields, multiple table query, delete duplicate record of the whole content of the method, I hope to help you, a lot of support cloud Habitat Community ~