需要查询以删除SQL中两个文件之间的重复项

时间:2013-01-01 15:31:32

标签: sql

SQL新手。

将两个文件导入声明表(声明)。文件4028有6,721行,文件2090有4,707行。两个文件都具有相同的列(Amt,First,Last,FillDate)。

使用以下查询来识别重复项:

SELECT first, last, amt, filldate, COUNT(*) AS duplicatecount
FROM Claims
WHERE fileid IN (4028, 2090)
GROUP BY first, last, amt, filldate
HAVING COUNT(*) > 1
ORDER BY 1,2 DESC

需要查询才能删除两个文件之间的重复项。从文件2090中删除。

由于

3 个答案:

答案 0 :(得分:0)

一种可能性是使用EXISTS来确保删除时存在重复。

DELETE C
FROM Claims C
WHERE C.fileId = 2090
AND EXISTS (
    SELECT 1
    FROM Claims D
    WHERE 
        D.fileid = 4028 
    AND C.first = D.first 
    AND C.last = D.last
    AND C.amt = D.amt
    AND C.filldate = D.filldate
)

答案 1 :(得分:0)

DELETE * FROM Claims WHERE fileid = 2090 AND first IN (the query you used to find the duplicates)

试试这个......请告诉我它是否有效......

答案 2 :(得分:0)

您可以使用相关子查询。

   delete * from yourtable a
   inner join 
   (select  fileid, first, last, amt, filldate             
    from yourtable
    where fileid in (4028, 2090)
    group by first, last, amt, filldate
    having count(1) > 1) as duplicates
   on (duplicates.first = a.first
   and duplicates.last = a.last
   and duplicates.amt = a.amt
   and duplicates.filldate = a.filldate)
   ;

即使没有定义要删除的文件ID,也会删除所有重复项:

delete * from yourtable a
   inner join 
   (select  min(id) minid, first, last, amt, filldate             
    from yourtable
    group by first, last, amt, filldate
    having count(1) > 1) as duplicates
   on (duplicates.first = a.first
   and duplicates.last = a.last
   and duplicates.amt = a.amt
   and duplicates.filldate = a.filldate
   and duplicates.minid <> a.id)
   ;