SQL Server查找重复项并插入到另一个表中

时间:2018-11-08 13:29:49

标签: sql-server

我在数据库中执行以下步骤。

 begin INSERT INTO
 tblDuplicate(PostingDate,DocumentDate,Reference,URN,Supplier,Amount,St,Check1,Check2,Check3,Check4,Check5,[Source])
 SELECT * FROM (
       SELECT h.PostingDate, h.DocumentDate, h.Reference, h.URN,h.Supplier,h.Amount, h.St,
 h.Check1,h.Check2,h.Check3,h.Check4,h.Check5,h.[Source]
       FROM tblHistory h
       WHERE h.Check1 IN(SELECT Check1 FROM tblTemp)
     UNION 
       SELECT t.PostingDate, t.DocumentDate, t.Reference, t.URN,t.Supplier,t.Amount, t.St,
 t.Check1,t.Check2,t.Check3,t.Check4,t.Check5, t.[Source]
       FROM tblTemp t
       WHERE t.Check1 IN(SELECT Check1 FROM tblHistory)
     ) as AllData   INSERT INTO tblDuplicate(PostingDate,DocumentDate,Reference,URN,Supplier,Amount,St,Check1,Check2,Check3,Check4,Check5,[Source])
 SELECT * FROM (
       SELECT h.PostingDate, h.DocumentDate, h.Reference, h.URN,h.Supplier,h.Amount, h.St,
 h.Check1,h.Check2,h.Check3,h.Check4,h.Check5,h.[Source]
       FROM tblHistory h
       WHERE h.Check2 IN(SELECT Check2 FROM tblTemp)
     UNION 
       SELECT t.PostingDate, t.DocumentDate, t.Reference, t.URN,t.Supplier,t.Amount, t.St,
 t.Check1,t.Check2,t.Check3,t.Check4,t.Check5, t.[Source]
       FROM tblTemp t
       WHERE t.Check2 IN(SELECT Check2 FROM tblHistory)
     ) as AllData INSERT INTO tblDuplicate(PostingDate,DocumentDate,Reference,URN,Supplier,Amount,St,Check1,Check2,Check3,Check4,Check5,[Source])
 SELECT * FROM (
       SELECT h.PostingDate, h.DocumentDate, h.Reference, h.URN,h.Supplier,h.Amount, h.St,
 h.Check1,h.Check2,h.Check3,h.Check4,h.Check5,h.[Source]
       FROM tblHistory h
       WHERE h.Check3 IN(SELECT Check3 FROM tblTemp)
     UNION 
       SELECT t.PostingDate, t.DocumentDate, t.Reference, t.URN,t.Supplier,t.Amount, t.St,
 t.Check1,t.Check2,t.Check3,t.Check4,t.Check5, t.[Source]
       FROM tblTemp t
       WHERE t.Check3 IN(SELECT Check3 FROM tblHistory)
     ) as AllData INSERT INTO tblDuplicate(PostingDate,DocumentDate,Reference,URN,Supplier,Amount,St,Check1,Check2,Check3,Check4,Check5,[Source])
 SELECT * FROM (
       SELECT h.PostingDate, h.DocumentDate, h.Reference, h.URN,h.Supplier,h.Amount, h.St,
 h.Check1,h.Check2,h.Check3,h.Check4,h.Check5,h.[Source]
       FROM tblHistory h
       WHERE h.Check4 IN(SELECT Check4 FROM tblTemp)
     UNION 
       SELECT t.PostingDate, t.DocumentDate, t.Reference, t.URN,t.Supplier,t.Amount, t.St,
 t.Check1,t.Check2,t.Check3,t.Check4,t.Check5, t.[Source]
       FROM tblTemp t
       WHERE t.Check4 IN(SELECT Check4 FROM tblHistory)
     ) as AllData INSERT INTO tblDuplicate(PostingDate,DocumentDate,Reference,URN,Supplier,Amount,St,Check1,Check2,Check3,Check4,Check5,[Source])
 SELECT * FROM (
       SELECT h.PostingDate, h.DocumentDate, h.Reference, h.URN,h.Supplier,h.Amount, h.St,
 h.Check1,h.Check2,h.Check3,h.Check4,h.Check5,h.[Source]
       FROM tblHistory h
       WHERE h.Check5 IN(SELECT Check5 FROM tblTemp)
     UNION 
       SELECT t.PostingDate, t.DocumentDate, t.Reference, t.URN,t.Supplier,t.Amount, t.St,
 t.Check1,t.Check2,t.Check3,t.Check4,t.Check5, t.[Source]
       FROM tblTemp t
       WHERE t.Check5 IN(SELECT Check5 FROM tblHistory)
     ) as AllData end

上面的过程主要检查检查1到检查5是否重复,然后将两个记录都发送到tblDuplicate。

请问有什么解决方案可以简化上述步骤。

表如下

PostingDate  DocumentDate Reference URN   Supplier  St   Check1
-------------------------------------------------------------------------
2018-10-05    12/09/2018   12345    12345   12345    2   12/09/201812345

所有表都包含相同的列。

该过程应查找check1,check2,check3.check4.check5的重复项,然后将结果插入tblDuplicate。

check1,check2,check3,check4和check 5的存储过程如下:

begin
UPDATE tblHistory SET Check1 = Supplier+Reference;
update tblHistory set Check2 = Supplier + DocumentDate + str(Amount,7,2) ;
update tblHistory set Check3 = Supplier + Reference + str(Amount,7,2);
update tblHistory set Check4 = Supplier +  DocumentDate + Reference + str(Amount,7,2);
update tblHistory set Check5 = DocumentDate + str(Amount,7,2) + Reference;
end

1 个答案:

答案 0 :(得分:0)

INSERT INTO tblDuplicate(PostingDate,DocumentDate,Reference,URN,Supplier,Amount,St,Check1,Check2,Check3,Check4,Check5,[Source])
    SELECT
        PostingDate,DocumentDate,Reference,URN,Supplier,Amount,St,Check1,Check2,Check3,Check4,Check5,[Source]
    FROM (
        SELECT h.PostingDate, h.DocumentDate, h.Reference, h.URN,h.Supplier,h.Amount, h.St,h.Check1,h.Check2,h.Check3,h.Check4,h.Check5,h.[Source]
        FROM tblHistory h
        INNER JOIN tblTemp tmp ON tmp.Check1=h.Check1
            OR tmp.Check2=h.Check2
            OR tmp.Check3=h.Check3
            OR tmp.Check4=h.Check4
            OR tmp.Check5=h.Check5
        UNION ALL
        SELECT h.PostingDate, h.DocumentDate, h.Reference, h.URN,h.Supplier,h.Amount, h.St,h.Check1,h.Check2,h.Check3,h.Check4,h.Check5,h.[Source]
        FROM tblTemp h
        INNER JOIN tblTemp tmp ON tmp.Check1=h.Check1
            OR tmp.Check2=h.Check2
            OR tmp.Check3=h.Check3
            OR tmp.Check4=h.Check4
            OR tmp.Check5=h.Check5
    ) AS AllData