I have a table "votes" with the following columns:
voter
, election_year
, election_type
, party
I need to remove all duplicate rows of the combination of voter
and election_year
, and I'm having trouble figuring out how to do this.
I ran the following:
WITH CTE AS(
SELECT voter,
election_year,
ROW_NUMBER()OVER(PARTITION BY voter, election_year ORDER BY voter) as RN
FROM votes
)
DELETE
FROM CTE where RN>1
based on another StackOverflow answer, but it seems this is specific to SQL Server. I've seen ways to do this using unique ID's, but this particular table doesn't have that luxury. How can I adopt the above script to remove the duplicates I need? Thanks!
EDIT: Per request, creation of the table with some example data:
CREATE TABLE public.votes
(
voter varchar(10),
election_year smallint,
election_type varchar(2),
party varchar(3)
);
INSERT INTO votes
(voter, election_year, election_type, party)
VALUES
('2435871347', 2018, 'PO', 'EV'),
('2435871347', 2018, 'RU', 'EV'),
('2435871347', 2018, 'GE', 'EV'),
('2435871347', 2016, 'PO', 'EV'),
('2435871347', 2016, 'GE', 'EV'),
('10215121/8', 2016, 'GE', 'ED')
;