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


It would probably be more efficient to do something like



DELETE FROM transaction t1
 WHERE EXISTS( SELECT 1
                 FROM transaction t2
                WHERE t1.date = t2.date
                  AND t1.refnumber = t2.refnumber
                  AND t1.parentFolderId = t2.parentFolderId
                  AND t2.id > t1.id )

      

+3


source


DELETE FROM transaction
      WHERE ID IN (
               SELECT ID
                 FROM (SELECT ID,
                          ROW_NUMBER () OVER (PARTITION BY  date
                                                          ,amount
                                                          ,refnumber
                                                          ,parentfolderid
                                                ORDER BY ID) rn
                                              FROM transaction)
                WHERE rn <> 1);

      



I'll try like this

+1


source


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







All Articles