I need to remove semi duplicate records from the following table
ID PID SCORE
1 1 50
2 33 20
3 1 90
4 5
WITH q AS
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY pid ORDER BY score) AS rn
FROM mytable
)
DELETE
FROM q
WHERE rn = 1
To leave the results that have no duplicates:
WITH q AS
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY pid ORDER BY score) AS rn,
COUNT(*) OVER (PARTITION BY pid) AS cnt
FROM mytable
)
DELETE
FROM q
WHERE rn = 1
AND cnt > 1