Home > Database > Mysql Tutorial > How to Efficiently Delete Duplicate Rows in Netezza Without a Unique Identifier?

How to Efficiently Delete Duplicate Rows in Netezza Without a Unique Identifier?

Barbara Streisand
Release: 2025-01-12 11:27:41
Original
535 people have browsed it

How to Efficiently Delete Duplicate Rows in Netezza Without a Unique Identifier?

Efficiently remove duplicate rows without unique identifiers in Netezza

When dealing with large tables containing duplicate rows, finding the most efficient way to remove them can be challenging. While this query has been proven to work in SQL, what about in Netezza?

Raw SQL query

WITH TempEmp AS
(
SELECT name, ROW_NUMBER() OVER(PARTITION by name, address, zipcode ORDER BY name) AS duplicateRecCount
FROM mytable
)
DELETE FROM TempEmp
WHERE duplicateRecCount > 1;
Copy after login

Netezza Solution

The DELETE statement after the WITH clause is not compatible with Netezza. Please consider the following solution using the USING keyword:

DELETE FROM table_with_dups T1
USING table_with_dups T2
WHERE T1.ctid < T2.ctid
AND T1.name = T2.name
AND T1.address = T2.address
AND T1.zipcode = T2.zipcode;
Copy after login

Preview results

To review records before deleting them, replace DELETE with SELECT * and USING with a comma, like this:

SELECT * FROM table_with_dups T1, table_with_dups T2
WHERE T1.ctid < T2.ctid
AND T1.name = T2.name
AND T1.address = T2.address
AND T1.zipcode = T2.zipcode;
Copy after login

Performance Notes

If few duplicates are expected, this solution performs better than the solution using the NOT IN (...) clause, which generates a large number of rows in the subquery. Additionally, if the key column contains NULL values, use COALESCE() to handle the comparison, for example:

AND COALESCE(T1.col_with_nulls, '[NULL]') = COALESCE(T2.col_with_nulls, '[NULL]')
Copy after login

The above is the detailed content of How to Efficiently Delete Duplicate Rows in Netezza Without a Unique Identifier?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template