SQL Server合并:如果源表中的两列不相等,则将它们作为新记录插入目标表的一列中

时间:2017-01-26 21:53:12

标签: sql-server sql-merge

我一直在努力解决一个问题,我需要比较源表中的两列,并将它们合并到第三个表中作为新行,如果不相等的话。如果它们相等则将它们作为一个记录插入示例下面:

来源表:

MIN_ID  MAX_ID
  1       1+
  4       2
  3       3
  3       1
  4       1+

所需目标表:

    MERGED_ID
     1
     1+
     4
     2
     3
     3
     1
     4
     1+

我尝试使用合并插入匹配值和一个非匹配值,将相同数量的记录插入目标(Source.MIN_ID或Source.MAX_ID)

    MERGE into [dbo].[Target] as A
    USING
      [dbo].[Source] as B
        ON
       (B.MIN_ID=B.MAX_ID)

  WHEN MATCHED THEN
    UPDATE SET A.MERGED_ID=B.MAX_ID

WHEN NOT MATCHED THEN
    INSERT
       (A.MERGED_ID)
 VALUES
       (B.MIN_ID)
       )
       ;
   GO

这会插入190万条记录 - 与源相同(如果等于MAX_ID,则为MIN_ID)。 但是我仍然需要将Source.MAX_ID值插入到此表中,如果不等于Source.MIN_ID所以我写了另一个类似的合并,它作为无限查询运行(55分钟直到我终止查询,目标表已经有190万行。合并花了13秒)

    MERGE into [dbo].[Target] as A
    USING
      [dbo].[Source] as B
        ON
       (A.MERGED_ID=B.MAX_ID)

  WHEN MATCHED THEN
    UPDATE SET A.MERGED_ID=B.MAX_ID

WHEN NOT MATCHED THEN
    INSERT
       (A.MERGED_ID)
 VALUES
       (B.MAX_ID)
       )
       ;
   GO

有人可以告诉我我做错了吗?

1 个答案:

答案 0 :(得分:1)

通过让事情变得更简单来解决我的问题。第一个查询修改如下

         MERGE into [dbo].[Target] as A
         USING
         [dbo].[Source] as B
          ON
           (B.MAX_ID=A.MERGED_ID)

         WHEN MATCHED THEN
              UPDATE SET A.MERGED_ID=B.MAX_ID

         WHEN NOT MATCHED THEN
             INSERT
                (A.MERGED_ID)
             VALUES
                (B.MIN_ID)
            ;
          GO

然后我使用一个简单的INSERT INTO SELECT STATEMENT来填充其他所需的值

    INSERT INTO dbo.Target
       (MERGED_ID)

    SELECT B.MAX_ID     
       FROM dbo.Source as B
       WHERE B.MIN_ID<>B.MAX_ID;

导致190万条原始记录加上450k额外记录,其中源表中的两列不相等