0

SQL新手。

将两个文件导入到索赔表 (Claims) 中。文件 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 中删除。

谢谢

4

3 回答 3

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
)
于 2013-01-01T15:39:49.487 回答
0
DELETE * FROM Claims WHERE fileid = 2090 AND first IN (the query you used to find the duplicates)

试试这个......如果它有效,请告诉我......

于 2013-01-01T15:43:12.687 回答
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)
   ;
于 2013-01-01T15:53:01.600 回答