gpt4 book ai didi

SQL删除表中的重复项

转载 作者:行者123 更新时间:2023-12-05 01:14:44 25 4
gpt4 key购买 nike

我有一个有重复的表事务。我想保留具有最小 ID 的记录,并根据四个字段 DATE、AMOUNT、REFNUMBER、PARENTFOLDERID 删除所有重复项。我写了这个查询,但我不确定这是否可以用有效的方式编写。你认为有更好的方法吗?我问是因为我担心运行时间。

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)

最佳答案

做这样的事情可能会更有效率

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 )

关于SQL删除表中的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10017027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com