How Does One Eliminate Duplicates Row

Embed Size (px)

Citation preview

  • 8/3/2019 How Does One Eliminate Duplicates Row

    1/3

    User login

    Login: *

    Password: *

    [ Register ] [ Forgotpassword ]

    Search

    [ Advanced ] [ Help ]

    Sitenavigation

    About

    Blogs

    Feed aggregator

    Books

    Directories

    Events

    FAQ's

    Forums

    Mailing Lists

    Papers

    Scripts

    Tools

    USENET News

    Wiki

    XML Feeds

    Similarentries

    How does oneselect EVERY Nthrow from a table?

    DifferencebetweenTRUNCATE,DELETE and DROPcommands

    Can one retrieveonly the Nth row

    Login

    Go

    Home Oracle FAQ Knowledge Base SQL and PL/SQL SQL

    How does one eliminate duplicates rows from a table?Submitted by admin on Wed, 2004-08-04 14:06

    Choose one of the following queries to identify or remove duplicate rows from a table leaving only unique

    records in the table:

    Method 1:

    Delete all rowids that is BIGGER than the SMALLEST rowid value (for a given key).

    Method 2:

    This method is usually faster. However, remember to recreate all indexes, constraints, triggers, etc. on the

    table when done.

    Method 3:

    (contributed by Dennis Gurnick)

    Note: One can eliminate N^2 unnecessary operations by creating an index on the joined fields in the inner

    loop (no need to loop through the entire table on each pass by a record). This will speed-up the deletion

    process.

    Note 2: If you are comparing NOT-NULL columns, use the NVL function. Remember that NULL is not equal to

    NULL. This should not be a problem as all key columns should be NOT NULL by definition.

    Login to post comments

    Submitted by William Robertson (not verified) on Mon, 2005-02-07 19:15.

    What about:

    SQL> DELETE FROM table_name A WHERE ROWID > (

    2 SELECT min(rowid) FROM table_name B

    3 WHERE A.key_values = B.key_values);

    SQL> create table table_name2 as select distinct * from table_name1;

    SQL> drop table table_name1;

    SQL> rename table_name2 to table_name1;

    SQL> delete from my_table t1

    SQL> where exists (select 'x' from my_table t2

    SQL> where t2.key_value1 = t1.key_value1

    SQL> and t2.key_value2 = t1.key_value2

    SQL> and t2.rowid > t1.rowid);

    How does one escape special characters

    when writing SQL queries?

    up How does one get the time difference

    between two date columns?

    Page 1 of 3How does one eliminate duplicates rows from a table? | Oracle FAQ

    21/Sep/2011http://www.orafaq.com/faq/how_does_one_eliminate_duplicates_rows_from_a_table

  • 8/3/2019 How Does One Eliminate Duplicates Row

    2/3

    from a table?

    Tuning a 'LIKE-clause' by usingOracle Text orReverse KeyIndexes

    How does onedrop/ rename acolumns in atable?

    Login to post comments

    Remove duplicate rows

    Submitted by srikanth M S (not verified) on Thu, 2006-02-23 13:16.

    Login to post comments

    Remove duplicate Rows (modified)

    Submitted by Srikanth M S (not verified) on Fri, 2006-02-24 05:57.

    This query removes rows based on the column names specified in the GROUP BY clause. If you specify onlyone column name it will remove all duplicate records for that column. If you want to delete exact replica's ofthe same row - use all the column names in the GROUP BY.

    Login to post comments

    Using dense_rank()

    Submitted by Seshagiri Vaddadi (not verified) on Sun, 2006-03-19 01:08.

    Another example using the dense_rank() function:

    Login to post comments

    Using dense_rank()

    Submitted by Srinivasan (not verified) on Thu, 2006-08-17 23:21.

    Col1 and col2 are the primary key columns. To provide the correct SQL to use alias name "ln" for f dense_rankin the statement:

    DELETE table_name

    WHERE rowid IN

    ( SELECT LEAD(rowid) OVER

    (PARTITION BY key_values ORDER BY NULL)

    FROM table_name );

    delete from where rowid not in

    ( select min(rowid)

    from exp group by column1..,column2,...column3..);

    delete from

    where rowid not in ( select min(rowid)

    from

    group by column1..,column2,...column3..)

    delete from table_name

    where rowid in

    (select rn from

    (select rowid rn, dense_rank() over (partition by col1, col2, ..order by rowid) from table_name ) where rn 1

    )

    Page 2 of 3How does one eliminate duplicates rows from a table? | Oracle FAQ

    21/Sep/2011http://www.orafaq.com/faq/how_does_one_eliminate_duplicates_rows_from_a_table

  • 8/3/2019 How Does One Eliminate Duplicates Row

    3/3

    .:: Blogger Home :: Wiki Home :: Forum Home :: Privacy :: Contact ::.

    Login to post comments

    deleting duplicate rows from a table

    Submitted by sriram (not verified) on Fri, 2006-05-19 01:24.

    This statement deletes rows from the emp table where duplicate values of last_name appear.

    Login to post comments

    This Command will delete all duplicate rows

    Submitted by varmas424 on Fri, 2010-03-05 23:05.

    Login to post comments

    Another Option

    Submitted by rajat2011 on Fri, 2011-06-17 00:25.

    It will be helpful especially when deletion is based on the date column:

    DELETE FROM Table t1 WHERE COLUMN_NAME < (SELECT MAX(COLUMN_NAME) FROM Table T2 WHERE T1.COL=T2.COL);

    Login to post comments

    delete from

    where rowid in (select rn from (Select rowid rn,

    dense_rank() over (partition by col1,col2.. order by rowid) ln

    from )

    where ln 1)

    delete from emp e

    where empno in(select empno from emp d

    where d.last_name=e.last_name

    minus

    select empno from emp f

    where f.last_name=e.last_name

    and rownum=1)

    delete from emp where ('Delete',sal) in (

    select case when count(*)>1 then 'Delete' else 'No' end DeleteFlag ,sal from emp group by sal having count(*)>1)

    Page 3 of 3How does one eliminate duplicates rows from a table? | Oracle FAQ

    21/Sep/2011http://www orafaq com/faq/how does one eliminate duplicates rows from a table