I have a table general_search_terms
with two columns term
, and dor
a date field. I need a postgres stored proceedure that checks for duplicate term
values and deletes the record with the latest dor
.
i.e. The goal is to basically delete the latest term if a duplicate of a given term is found.
Does this look right, or is there a better way to do this?
CREATE PROCEDURE delete_duplicate_terms()
LANGUAGE plpgsql
AS $$
BEGIN
DELETE FROM general_search_terms
WHERE (term, dor) IN (
SELECT term, MAX(dor)
FROM general_search_terms
GROUP BY term
HAVING COUNT(*) > 1
);
END;
$$;