Removing Duplicate Rows in PostgreSQL with multiple columns
Here's an option
DELETE FROM votes T1 USING votes T2WHERE T1.ctid < T2.ctid AND T1.voter = T2.voter AND T1.election_year = T2.election_year;
Delete from or updating CTEs doesn't work in Postgres, see the accepted answer of "PostgreSQL with-delete “relation does not exists”".
Since you have no primary key you may (ab)use the ctid
pseudo column to identify the rows to delete.
WITHcteAS(SELECT ctid, row_number() OVER (PARTITION BY voter, election_year ORDER BY voter) rn FROM votes)DELETE FROM votes USING cte WHERE cte.rn > 1 AND cte.ctid = votes.ctid;
And probably think about introducing a primary key.
The ctid field is a field that exists in every PostgreSQL table and is unique for each record in a table and denotes the location of the tuple.You did almost right just need ctid as you have no unique id for each row
;WITH CTE AS(SELECT ctid,voter, election_year, ROW_NUMBER()OVER(PARTITION BY voter, election_year ORDER BY voter) as RNFROM votes)delete FROM votes v where v.ctid in (select CTE.ctid from CTE where CTE.RN>1)