Query to eliminate dulicate rows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ranjitkumar
    New Member
    • Nov 2006
    • 5

    Query to eliminate dulicate rows

    Hi,

    Do anyone know a query for the following senario,

    I have a table in which i have multiple rows with the same entry. I want to keep only one copy of such rows. How to write a query to delete all the duplicate rows and have only one entry for such with a single query???

    For example
    [code=oracle]
    create table Employee
    {
    empno char(5),
    name varchar2(20),
    salary number(5,2)
    };
    [/code]

    the content of the table is

    emp01 ranjit 10000.50
    emp01 ranjit 10000.50
    emp01 ranjit 10000.50
    emp01 ranjit 10000.50
    emp01 ranjit 10000.50
    emp01 ranjit 10000.50

    how to delete the duplicate entries in the above table ????
    Last edited by amitpatel66; Nov 15 '07, 08:28 AM. Reason: code tags
  • minapatel
    New Member
    • Oct 2006
    • 12

    #2
    Originally posted by ranjitkumar
    Hi,

    Do anyone know a query for the following senario,

    I have a table in which i have multiple rows with the same entry. I want to keep only one copy of such rows. How to write a query to delete all the duplicate rows and have only one entry for such with a single query???

    For example

    create table Employee
    {
    empno char(5),
    name varchar2(20),
    salary number(5,2)
    };

    the content of the table is

    emp01 ranjit 10000.50
    emp01 ranjit 10000.50
    emp01 ranjit 10000.50
    emp01 ranjit 10000.50
    emp01 ranjit 10000.50
    emp01 ranjit 10000.50

    how to delete the duplicate entries in the above table ????

    Try where the row count is more than one delete the extra rows....

    Comment

    • VitorLeite
      New Member
      • Nov 2006
      • 1

      #3
      Originally posted by ranjitkumar
      Hi,

      Do anyone know a query for the following senario,

      I have a table in which i have multiple rows with the same entry. I want to keep only one copy of such rows. How to write a query to delete all the duplicate rows and have only one entry for such with a single query???

      For example

      create table Employee
      {
      empno char(5),
      name varchar2(20),
      salary number(5,2)
      };

      the content of the table is

      emp01 ranjit 10000.50
      emp01 ranjit 10000.50
      emp01 ranjit 10000.50
      emp01 ranjit 10000.50
      emp01 ranjit 10000.50
      emp01 ranjit 10000.50

      how to delete the duplicate entries in the above table ????

      Have you tried using
      [code=oracle]
      SELECT DISTINCT(empno) FROM Employee
      [/code]
      Oh, about the deletion part, i'm not really sure, but you should save the results of the 1st query, truncate the table, and insert them back in it again.

      Hope it helps
      Last edited by amitpatel66; Nov 15 '07, 08:30 AM. Reason: code tags

      Comment

      • rkamuni
        New Member
        • Nov 2006
        • 3

        #4
        Hi, You can use the follwoing query.
        [code=oracle]
        delete from employee e where e.rowid not in ( select min(b.rowid) from employee b where e.primarykeyfld = b.primarykeyfld )
        [/code]

        Hope this will solve ur issue.

        Thanks,

        Originally posted by ranjitkumar
        Hi,

        Do anyone know a query for the following senario,

        I have a table in which i have multiple rows with the same entry. I want to keep only one copy of such rows. How to write a query to delete all the duplicate rows and have only one entry for such with a single query???

        For example

        create table Employee
        {
        empno char(5),
        name varchar2(20),
        salary number(5,2)
        };

        the content of the table is

        emp01 ranjit 10000.50
        emp01 ranjit 10000.50
        emp01 ranjit 10000.50
        emp01 ranjit 10000.50
        emp01 ranjit 10000.50
        emp01 ranjit 10000.50

        how to delete the duplicate entries in the above table ????
        Last edited by amitpatel66; Nov 15 '07, 08:29 AM. Reason: code tags

        Comment

        • ck1004
          New Member
          • Nov 2006
          • 3

          #5
          Originally posted by ranjitkumar
          Hi,

          Do anyone know a query for the following senario,

          I have a table in which i have multiple rows with the same entry. I want to keep only one copy of such rows. How to write a query to delete all the duplicate rows and have only one entry for such with a single query???

          For example

          create table Employee
          {
          empno char(5),
          name varchar2(20),
          salary number(5,2)
          };

          the content of the table is

          emp01 ranjit 10000.50
          emp01 ranjit 10000.50
          emp01 ranjit 10000.50
          emp01 ranjit 10000.50
          emp01 ranjit 10000.50
          emp01 ranjit 10000.50

          how to delete the duplicate entries in the above table ????
          [code=oracle]
          delete from employee where rowid not in (select max(rowid) from employee group by empno);
          [/code]
          or
          [code=oracle]
          delete from Employee emp where rowid <(select min(rowid) from Employee emp1 where emp.empno=emp12 .empno);
          [/code]
          Last edited by amitpatel66; Nov 15 '07, 08:31 AM. Reason: code tags again

          Comment

          • ck1004
            New Member
            • Nov 2006
            • 3

            #6
            [QUOTE=ranjitkum ar]Hi,

            Do anyone know a query for the following senario,

            I have a table in which i have multiple rows with the same entry. I want to keep only one copy of such rows. How to write a query to delete all the duplicate rows and have only one entry for such with a single query???

            For example

            create table Employee
            {
            empno char(5),
            name varchar2(20),
            salary number(5,2)
            };

            the content of the table is

            emp01 ranjit 10000.50
            emp01 ranjit 10000.50
            emp01 ranjit 10000.50
            emp01 ranjit 10000.50
            emp01 ranjit 10000.50
            emp01 ranjit 10000.50

            how to delete the duplicate entries in the above table ????[/delete from employee where rowid not in (select max(rowid) from employee group by empno);
            or
            delete from employee emp where rowid <(select min(rowid) from employee emp1 where emp.empno=emp1. empno); ]

            Comment

            • pragatiswain
              Recognized Expert New Member
              • Nov 2006
              • 96

              #7
              I am not really sure what you are really looking for.
              Some times I do the following. It is much faster with huge data volume (Millions of records) and safe.
              1. Insert distinct records into a backup table.
              2. Truncate Original table.
              3. Insert records in original table from backuptable.

              Hope this helps

              Comment

              • suvam
                New Member
                • Nov 2006
                • 31

                #8
                u may try with the either one to delete duplicate rows --->

                [code=oracle]
                1. Delete from T1 a
                where a.rowid !=(Select min(b.rowid) from T1 b where b.pk_key = a.pk_key) ;
                [/code]
                [code=oracle]
                2. delete from T1
                group by pk_key
                having count(pk_key) > 1 ;
                [/code]
                Last edited by amitpatel66; Nov 15 '07, 08:32 AM. Reason: code tags again

                Comment

                • akhileshkumar18
                  New Member
                  • Nov 2007
                  • 1

                  #9
                  Originally posted by ranjitkumar
                  Hi,

                  Do anyone know a query for the following senario,

                  I have a table in which i have multiple rows with the same entry. I want to keep only one copy of such rows. How to write a query to delete all the duplicate rows and have only one entry for such with a single query???

                  For example

                  create table Employee
                  {
                  empno char(5),
                  name varchar2(20),
                  salary number(5,2)
                  };

                  the content of the table is

                  emp01 ranjit 10000.50
                  emp01 ranjit 10000.50
                  emp01 ranjit 10000.50
                  emp01 ranjit 10000.50
                  emp01 ranjit 10000.50
                  emp01 ranjit 10000.50

                  how to delete the duplicate entries in the above table ????
                  Hi Ranjit,
                  You can try this
                  [code=oracle]
                  delete from Employee
                  where empno ='emp01'
                  AND name ='ranjit'
                  AND salary = 10000.50
                  AND rownum = 1
                  [/code]

                  Run this query one less then the no of records.
                  In this example there are 6 entries of the same so you have to run this query 5 times

                  Regards
                  Akhilesh
                  Last edited by amitpatel66; Nov 15 '07, 08:33 AM. Reason: code tags

                  Comment

                  • amitpatel66
                    Recognized Expert Top Contributor
                    • Mar 2007
                    • 2358

                    #10
                    All,

                    Please enclose your posted code in [code] tags (See How to Ask a Question).

                    This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

                    Please use [code] tags in future.

                    MODERATOR

                    Comment

                    • amitpatel66
                      Recognized Expert Top Contributor
                      • Mar 2007
                      • 2358

                      #11
                      Try below query:

                      [code=oracle]
                      DELETE FROM table1 WHERE ROWID NOT IN (SELECT MAX(ROWID) FROM table1 GROUP BY empno HAVING COUNT(empno) > 1)
                      [/code]

                      The above query will delete all the older duplicate rows from the table keeping the latest inserted record in the table

                      Comment

                      Working...