3

I need help with duplicate rows. I have deleted the duplicate rows from one table using the following method

DELETE FROM names a
WHERE ROWID > (SELECT MIN(ROWID) FROM names b
WHERE b.name=a.name
AND b.age=a.age
);

It did work with that particular table but I did the same thing with another table which has duplicate reference numbers for each doctor but different unique codes.

doc_name  ref_no  unique_code
------------------------------
abcd      1010     1111
abcd      1010     1112
cdef      1011     1113
cdef      1011     1114

My result should look like this:

doc_name ref_no unique_code
---------------------------
abcd     1010      1111
cdef     1011      1113
1
  • 1
    what is the query you used for the 2nd query? Commented May 31, 2015 at 19:16

4 Answers 4

4

You can use ROW_NUMBER(), to detect duplicate rows and delete them.

DELETE tblName
WHERE ROWID IN (
    SELECT ROWID
    FROM(
      SELECT ROW_NUMBER() OVER (PARTITION BY doc_name, ref_no ORDER BY doc_name, ref_no) AS Rn
            ,doc_name
            ,ref_no
            ,unique_code
      FROM tblName
    )
    WHERE Rn > 1
   )
Sign up to request clarification or add additional context in comments.

4 Comments

You can't delete from a CTE in Oracle
@sqluser i tried your query.it showed me the following error: WITH C AS( SELECT ROW_NUMBER() OVER (PARTITION BY doc_name, ref_no ORDER BY doc_name, ref_no) AS Rn , doc_name , ref_no , unq_cd FROM doc_unique ) DELETE FROM C WHERE Rn > 1 SQL> / DELETE FROM C WHERE Rn > 1 * ERROR at line 8: ORA-00928: missing SELECT keyword
@Iftekhar, as @a_horse_with_no_name mentioned in his comment we cannot delete from a CTE. So I changed the query. Please see the updated. Thanks to @a_horse_with_no_name for pointing it out.
@sqluser,thanks for the updated query.i have changed the new query a little as there is no ref_no in the table where i am looking to delete the duplicate rows.will the new query work or should i include father's name(ft_nm) and mother's name(mt_nm) too? DELETE t_doc_unique WHERE ROWID IN ( SELECT ROWID FROM( SELECT ROW_NUMBER() OVER (PARTITION BY doc_name,unq_cd ORDER BY doc_name, unq_cd) AS Rn ,doc_name ,unq_cd FROM t_doc_unique ) WHERE Rn > 1 ) /
2

Did you try it like this ?

DELETE FROM names a
WHERE ROWID > (SELECT MIN(ROWID) FROM names b
WHERE b.doc_name=a.doc_name
AND b.ref_no=a.ref_no
)

try this also

SELECT *
  FROM doc_unique
 WHERE (DIV_CD, DOC_NAME, B_DT, FT_NM, UNQ_CD, DESG_CD,
 SPEC_CD) IN (SELECT DIV_CD, DOC_NAME, B_DT, FT_NM, UNQ_CD, DESG_CD,
 SPEC_CD
                             FROM doc_unique
                            GROUP BY DIV_CD, DOC_NAME, B_DT, FT_NM, UNQ_CD, DESG_CD,
 SPEC_CD HAVING COUNT(*) > 1)

3 Comments

Moudiz,i did a manual search on the table and also checked for duplicate values but there was none could be found. can you tell me if this is the proper way to check for duplicate rows select DIV_CD,DOC_NAME,B_DT,FT_NM,UNQ_CD, DESG_CD,SPEC_CD,count(*) from doc_unique group by DIV_CD,DOC_NAME,B_DT,FT_NM, UNQ_CD,DESG_CD,SPEC_CD having count(unq_cd)>1
@Iftekhar yes it seems good however check this query , its another way to check duplicate
your query to check for duplicates worked.thanks a lot
1

Please, try exists

delete from names a
where exists (
  select * 
  from names b
  where b.name = a.name
        and b.age = a.age
        and a.unique_code > b.unique_code 
)

Comments

0

you can delete duplicate data by using following sql query:

delete from [table_name] where rowid in ( select max(rowid) from [table_name] group by [column_name] );

Note: [table_name] like STUDENT and [column_name] like STUD_ID

Find Screenshot

2 Comments

Avoid sharing screenshots, link might get broken throughout time
Thank you for sharing, I will take care from now @atish.s

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.