Tuesday, August 23, 2011

Duplicate rows from Oracle table

Remove duplicated including rows having "null"values

DELETE
FROM table_name t1
WHERE t1.rowid > ANY
  (SELECT t2.rowid
  FROM table_name t2
  WHERE (t1.col1 = t2.col1
  OR (t1.col1   IS NULL
  AND t2.col1   IS NULL))
  AND (t1.col2   = t2.col2
  OR (t1.col2   IS NULL
  AND t2.col2   IS NULL))
  );

Find and delete duplicate rows

DELETE
FROM table_name t1
WHERE rowid <>
  (SELECT MAX(rowid)
  FROM table_name t2
  WHERE t1.col1 = t2.col1
  AND t1.col2   = t2.col2
  );

No comments:

Post a Comment