SQL to delete the duplicates in a table

余生颓废 提交于 2019-12-01 09:41:31

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 )
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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!