在SQL Server 2008中将非重复数据从父表插入子表

时间:2016-03-29 12:17:53

标签: sql sql-server sql-server-2008

我有两个表:A和B在两个表上都有相同的列:

Customer_Part_Number,
Lear_Part_Number,
Shipping_ID,
Customer_Name,
Effective_Date,
End_Date,Change_ID,
PO_Number,
PO_Price

我已成功将所有数据从表A复制到表B. 但我的情况是列的记录:

TENANT_ID,          
CUSTOMER_PART_NUMBER,       
SHIPPING_ID,       
EFFECTIVE_DATE         
表中的

是相同的,我不希望它将它插入到表B中。 我尝试过使用查询:

INSERT INTO OSUSR_1SV_QAD_PO_DATA 
    (TENANT_ID,
     CUSTOMER_PART_NUMBER,
     LEAR_PART_NUMBER,
     SHIPPING_ID,
     CUSTOMER_NAME,
     PROGRAM_NAME,
     EFFECTIVE_DATE,
     END_DATE,
     CHANGE_ID,
     PO_NUMBER,
     PO_PRICE)
SELECT 
     TENANT_ID,
     CUSTOMER_PART_NUMBER,
     LEAR_PART_NUMBER,
     SHIPPING_ID,
     CUSTOMER_NAME,
     PROGRAM_NAME,
     EFFECTIVE_DATE,
     END_DATE,
     CHANGE_ID,
     PO_NUMBER,
     PO_PRICE 
FROM 
OSUSR_1SV_STAGING_FTP A
WHERE 
not exists
 ( SELECT TENANT_ID,
     CUSTOMER_PART_NUMBER,
     LEAR_PART_NUMBER,
     SHIPPING_ID,
     CUSTOMER_NAME,
     PROGRAM_NAME,
     EFFECTIVE_DATE,
     END_DATE,
     CHANGE_ID,
     PO_NUMBER,
     PO_PRICE
     FROM OSUSR_1SV_QAD_PO_DATA B
     WHERE
      A.TENANT_ID = B.TENANT_ID
       and A.CUSTOMER_PART_NUMBER = A.CUSTOMER_PART_NUMBER
       and A.SHIPPING_ID = B.SHIPPING_ID
       and  A.EFFECTIVE_DATE = B.EFFECTIVE_DATE )

此处,如果列TENANT_ID,CUSTOMER_PART_NUMBER,SHIPPING_ID,EFFECTIVE_DATE的记录相同,则不会插入重复记录。

请帮助我。

enter image description here

在第一张图片中,OSUSR_1SV_STAGING_FTP表上有重复数据的基础 TENANT_ID,
CUSTOMER_PART_NUMBER,
SHIPPING_ID,
EFFECTIVE_DATE

但是我需要在表OSUSR_1SV_QAD_PO_DATA上插入像Image 2这样的数据而不重复 TENANT_ID,
CUSTOMER_PART_NUMBER,
SHIPPING_ID,
EFFECTIVE_DATE
enter image description here

我试过这个查询
MERGE OSUSR_1SV_QAD_PO_DATA A
使用OSUSR_1SV_STAGING_FTP B
ON(B.TENANT_ID = A.TENANT_ID和B.CUSTOMER_PART_NUMBER = A.CUSTOMER_PART_NUMBER
和B.SHIPPING_ID = A.SHIPPING_ID和B.EFFECTIVE_DATE = A.EFFECTIVE_DATE)
什么时候匹配呢 DELETE WHERE ID NOT IN(选择MIN(ID)_
来自OSUSR_1SV_STAGING_FTP
GROUP BY CUSTOMER_PART_NUMBER,SHIPPING_ID)
当没有匹配时 INSERT(TENANT_ID,CUSTOMER_PART_NUMBER,LEAR_PART_NUMBER,SHIPPING_ID,CUSTOMER_NAME,PROGRAM_NAME,EFFECTIVE_DATE,END_DATE,CHANGE_ID,PO_NUMBER,PO_PRICE)
VALUES(B.TENANT_ID,B.CUSTOMER_PART_NUMBER,B.LEAR_PART_NUMBER,B.SHIPPING_ID,
B.CUSTOMER_NAME,B.PROGRAM_NAME,B.EFFECTIVE_DATE,B.END_DATE,B.CHANGE_ID,B.PO_NUMBER,B.PO_PRICE);

2 个答案:

答案 0 :(得分:1)

替换此部分

not exists
( SELECT 1
 FROM OSUSR_1SV_QAD_PO_DATA B
 WHERE
  A.TENANT_ID = B.TENANT_ID
   --and A.CUSTOMER_PART_NUMBER = A.CUSTOMER_PART_NUMBER --you are Comparing same value here
   and A.CUSTOMER_PART_NUMBER = B.CUSTOMER_PART_NUMBER  
   and A.SHIPPING_ID = B.SHIPPING_ID
   and  CAST(A.EFFECTIVE_DATE AS DATE) = CAST(B.EFFECTIVE_DATE AS DATE) )

答案 1 :(得分:0)

尝试此查询:

        INSERT INTO OSUSR_1SV_QAD_PO_DATA 
(TENANT_ID,
 CUSTOMER_PART_NUMBER,
 LEAR_PART_NUMBER,
 SHIPPING_ID,
 CUSTOMER_NAME,
 PROGRAM_NAME,
 EFFECTIVE_DATE,
 END_DATE,
 CHANGE_ID,
 PO_NUMBER,
 PO_PRICE)

Select TENANT_ID,
 CUSTOMER_PART_NUMBER,
 LEAR_PART_NUMBER,
 SHIPPING_ID,
 CUSTOMER_NAME,
 PROGRAM_NAME,
 EFFECTIVE_DATE,
 END_DATE,
 CHANGE_ID,
 PO_NUMBER,
 PO_PRICE 
 from OSUSR_1SV_STAGING_FTP A

 INNER JOIN (select TENANT_ID,CUSTOMER_PART_NUMBER,SHIPPING_ID,EFFECTIVE_DATE from OSUSR_1SV_STAGING_FTP 
    group by TENANT_ID,CUSTOMER_PART_NUMBER,SHIPPING_ID,EFFECTIVE_DATE) as NoDuplicate ON A.TENANT_ID=NoDuplicate.TENANT_ID