简体   繁体   English

避免在 SQL 服务器中的 INSERT INTO SELECT 查询重复

[英]Avoid duplicates on INSERT INTO SELECT query in SQL Server

How can I avoid duplicate using the provided below query如何使用下面提供的查询避免重复

INSERT INTO dbo.Entities(EntityId, [Name], [Description], [Type], Source)
    SELECT DISTINCT 
        CUST_CODE, NAME, FULLDESCRIPTION, 'Agency' AS Type, 'SunDbAgencies' AS Source
    FROM dbo.VW_SUNDB_AGENCIES 

I've already tried all the answers here:我已经在这里尝试了所有答案:

Avoid duplicates in INSERT INTO SELECT query in SQL Server to no avail. 避免在 SQL 服务器中的 INSERT INTO SELECT 查询中重复,但无济于事。

The duplicates are in the dbo.VW_SUNDB_AGENCIES TABLES not the INSERT table, So I gather I need a way to remove duplicate from the select before inserting重复项在dbo.VW_SUNDB_AGENCIES TABLES而不是INSERT表中,所以我认为我需要一种方法在插入之前从 select 中删除重复项

Here is one of the duplicates which is why a simple distinct doesn't work:这是重复项之一,这就是为什么简单的 distinct 不起作用的原因:

在此处输入图像描述

在此处输入图像描述

The problem is that select distinct is not sufficient.问题是select distinct是不够的。 You still have duplicates in the underlying table, but with different names or descriptions.基础表中仍有重复项,但名称或描述不同。

I view this as a problem.我认为这是一个问题。 But, you can work around it by selecting one arbitrary row per cust_code , using row_number() :但是,您可以通过使用row_number()为每个cust_code选择任意一行来解决它:

insert into dbo.Entities (EntityId, [Name], [Description], [Type], Source)
  select CUST_CODE, NAME, FULLDESCRIPTION, 'Agency' AS [Type], 'SunDbAgencies' AS Source
  from (select a.*,
               row_number() over (partitoin by cust_code order by cust_code) as seqnum
        from dbo.VW_SUNDB_AGENCIES a
       ) a
  where seqnum = 1 and
        not exists (select 1 from dbo.Entities E where A.CUST_CODE = E.EntityId);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM