SQL to remove duplicates in a table
I have a table transaction with duplicates. I want to keep the record with the minimum ID and remove all duplicates based on the four fields DATE, AMOUNT, REFNUMBER, PARENTFOLDERID. I wrote this request, but I'm not sure if it can be written in an efficient way. Do you think there is a better way? I am asking because I am worried about the runtime.
DELETE FROM TRANSACTION
WHERE ID IN
(SELECT FIT2.ID
FROM
(SELECT MIN(ID) AS ID, FIT.DATE, FIT.AMOUNT, FIT.REFNUMBER, FIT.PARENTFOLDERID
FROM EWORK.TRANSACTION FIT
GROUP BY FIT.DATE, FIT.AMOUNT , FIT.REFNUMBER, FIT.PARENTFOLDERID
HAVING COUNT(1)>1 and FIT.AMOUNT >0) FIT1,
EWORK.TRANSACTION FIT2
WHERE FIT1.DATE=FIT2.DATE AND
FIT1.AMOUNT=FIT2.AMOUNT AND
FIT1.REFNUMBER=FIT2.REFNUMBER AND
FIT1.PARENTFOLDERID=FIT2.PARENTFOLDERID AND
FIT1.ID<>FIT2.ID)
+3
source to share
3 answers
I would try something like this:
DELETE transaction
FROM transaction
LEFT OUTER JOIN
(
SELECT MIN(id) as id, date, amount, refnumber, parentfolderid
FROM transaction
GROUP BY date, amount, refnumber, parentfolderid
) as validRows
ON transaction.id = validRows.id
WHERE validRows.id IS NULL
0
source to share