IHAC that loads data with primary key duplication and wants to remove the duplicate rows. The entire row isn't a duplicate, only the key value. They want to find the rows with duplicate keys and remove all violating rows except the first row. I created the below solution, but then realized Vertica doesn't support deleting from a CTE. Anybody have an alternate way of doing this:
Select * from test:
col1 col2
------ ------
1 20
1 10
2 10
2 30
In this example, I want to delete the 2nd and 4th row, so:
With dup as
--find the duplicates
(SELECT *, row_number() over () rownumber
FROM test t
WHERE EXISTS (
SELECT 1
FROM test t1
WHERE t1.col1 = t.col1
and t1.col2 != t.col2
) ),
--Order the rows
x
AS
(
SELECT col1, col2,
DENSE_RANK() OVER (PARTITION BY col1 order by rownumber ) rn
FROM dup
)
delete *
FROM x WHERE
rn != 1;