Oracle record duplication Processing

Source: Internet
Author: User

Oracle repeat processing already has some simple SQL on the internet for use, but I feel there are some improvements.

Delete Table1 where rowid not in (
Select max (rowid) from Table1 group by col1, col2)

The preceding method deletes duplicate information and retains the record with the largest rowid in the same field.

However, this method needs to be improved to query numbers without duplicates. Readers may think that distinct can be used to deduplicate the number. However, you need to record the number again.

This article briefly introduces some SQL statements for Repeated Records, only for Oracle.

 

Query duplicate records:

Select * From yourtable where key in (

Select key from yourtable group by key having count (key)> 1)

Order by key

Query the result set for removing duplicate records:

Select *

From (select T. *, row_number () over (partition by key order by field1) rnk

From yourtable t

Order by key) ot

Where rnk = 1

Here, it is more flexible to rank the group information to retrieve records that should be retained. In the preceding example, key is grouped and sorted by field1 field to flexibly select useful information.

With query, it is easy to import duplicate records to another table:

Create Table another_table

Select .....

 

Back to question: What if I delete Repeated Records?

You can use the keyword of the query result as the deletion condition:

Delete table yourtable where key in (

Select key

From (select key, row_number () over (partition by key order by field1) rnk

From yourtable t

Order by key) ot

Where rnk> 1)

It seems much more complicated than the solution provided by others, but it only adds a flexible option for Repeated Records.

This is just another way of thinking. Please give your comments and correct them.

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.