Can we delete duplicate records from a table in teradata without using intermediate table

半世苍凉 提交于 2019-11-28 14:27:38

You can't unless the ROWID usage has been enabled on your system (and probablity is quite low). You can easily test it by trying to explain a SELECT ROWID FROM table;

Otherwise there are two possible ways.

Low number of duplicates:

  • create a new table as result of SELECT all columns FROM table GROUP BY all columns HAVING COUNT(*) > 1;
  • DELETE FROM tab WHERE EXISTS (SELECT * FROM newtab WHERE...)
  • INSERT INTO tab SELECT * FROM newtab

High number of duplicates:

  • copy to a new table using SELECT DISTINCT * or copy to a SET TABLE to get rid of the duplicates and then re-INSERT back

Use the same approach, but create a volatile table in the middle.

CREATE VOLATILE MULTISET TABLE TEMPDB.TEMP_DUP_ID (
Row_ID DECIMAL(31,0) ) PRIMARY INDEX (Row_ID) ON COMMIT PRESERVE ROWS;

INSERT INTO TEMPDB.TEMP_DUP_ID SELECT ROW_ID FROM DB.TABLE T QUALIFY ROW_NUMBER() OVER (PARTITION BY DUP ORDER BY DUP DESC) > 1

Then use the table to delete.

Ideally you will have unique key per row, otherwise, you will need to manipulate the data a bit more to generate one (with row_number() for instance... This is just a recommendation).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!