SQL to delete the duplicates in a table

后端 未结 3 1689
失恋的感觉
失恋的感觉 2021-01-14 13:08

I have a table transaction which has duplicates. i want to keep the record that had minimum id and delete all the duplicates based on four fields DATE, AMOUNT, REFNUMBER, PA

相关标签:
3条回答
  • 2021-01-14 13:26

    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 讨论(0)
  • 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 will try like this

    0 讨论(0)
  • 2021-01-14 13:47

    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 )
    
    0 讨论(0)
提交回复
热议问题