Parsing Oracle query and deleting SQL of JOB

  • 2021-08-17 01:19:17
  • OfStack

SQL statement for querying and deleting duplicate records
1. Lookup the redundant duplicate records in the table. The duplicate records are judged according to a single field (peopleId)
select * from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)

2. Delete redundant duplicate records in the table. The duplicate records are judged according to a single field (peopleId), and only the records with the smallest rowid are left
delete from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
and rowid not in (select min(rowid) from people group by peopleId having count(peopleId ) > 1)
Note: rowid comes with oracle and does not need this......
3. Redundant duplicate records (multiple fields) in the lookup table
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

4. Delete redundant duplicate records (multiple fields) in the table, leaving only the records with the smallest rowid
delete from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*) > 1)
5. Lookup the redundant duplicate records (multiple fields) in the table, excluding the records with the smallest rowid
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*) > 1)
(2)
For example
There is a field "name" in the A table,
And the "name" value may be the same between different records,
Now it is necessary to find out the items with duplicate values of "name" between the records in the table;
Select Name,Count(*) from A Group By Name Having Count(*) > 1
If the gender is also the same, it is as follows:
Select Name,sex,Count(*) from A Group By Name,sex Having Count(*) > 1
(3)
Method 1
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
open cur_rows
fetch cur_rows into @id,@max
while @@fetch ......

Related articles: