Duplicate data method in Oracle query table

Source: Internet
Author: User

Select ID from the group by ID have count (*) > 1

Grouped by ID and counted, an ID number that has more than 1 of the group is considered duplicates.

How to query for duplicate data

Select field 1, Field 2,count (*) from table name Group By field 1, Field 2 having count (*) > 1

PS: The above > is changed to = number can be queried for no duplication of data.

Oracle Delete duplicate data SQL (delete all):

Basic structure for deleting duplicate data:

To delete these duplicated data, you can delete them using the following statement

Delete from table name a where field 1, Field 2 in
(select field 1, Field 2,count (*) from table name Group By field 1, Field 2 having count (*) > 1)

The above SQL Note: The statement is very simple, that is, the query to delete the data. However, this removal execution is inefficient and may hang the database for large data volumes.

It is recommended that you insert the duplicate data that you query into a temporary table, and then delete it so that you do not have to query again when you perform the delete. As follows:

CREATE Table Temp Table as (select field 1, Field 2,count (*) from table name Group By field 1, Field 2 having count (*) > 1)

The above phrase is to create a temporary table and insert the queried data into it.

Here's how to do this:

Delete from table name a where field 1, Field 2 in (Select field 1, Field 2 from temp table);

Oracle Delete duplicate data SQL (leave a record):

Oracle, there is a hidden automatic rowid, which gives each record a unique rowid, if we want to keep the latest record, we can use this field, to keep the duplicate data rowid the largest record.

To query for duplicate data using ROWID:

Select a.rowid,a.* from table name a
where A.rowid! =
(select Max (b.rowid) from table name B
Where a. Field 1 = B. Field 1 and
A. Field 2 = B. Field 2)

The SQL query in parentheses rowid the largest record, while the outside is querying out the duplicate data except the maximum of ROWID.

Thus, we want to delete the duplicate data and keep only the latest data, so we can write:

Delete duplicate data (leave one of the maximum rowid)

Delete from table name a WHERE a.rowid! =
(select Max (b.rowid) from table name B
Where a. Field 1 = B. Field 1 and
A. Field 2 = B. Field 2)

Delete duplicate data (leave one of the smallest rowid)

Delete tab T where T.rowid > (
Select min (t2.rowid) from tab t2 where T.col2 = t2.col2 and T.col8 = T2.col8
)

Duplicate data method in Oracle query table

Related Article

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.