0

我有一个要求,我需要为特定 EmpID 插入具有不同哈希值的记录。以下是暂存记录和主记录中的一些示例值。Staging 每周获取值,需要根据 ID 和 hashvalue 将值插入到 master 中,当所有记录都被处理时,表被截断。

Week 1 in staging: EMpd ID Hash Value
    1000    1; 2000    2
2 records gets inserted in master

Week2 in staging: EMPID hashvalues
              1000   -3
              2000    2
              3000    5
Master should have 1000  1;1000   -3; 2000 2; 3000  5
Week 3 in staging: EMPID hashvalues
               1000  -5
               2000  -9
               3000   5
Master should have 1000  1;1000   -3; 1000   -5;2000 2; 2000 -9;3000  5


I have tried doing 
insert into EMP_MASTER (EMPID,EMPNAME,hashValue)
select mas.*,stg.hashvalue FROM [EMP_master] mas (nolock) 
INNER JOIN [EMP_staging] stg (nolock) ON mas.EMPID = stg.EMPID
WHERE mas.hashvalue != stg.hashvalue and not exists 
(select 1 from [EMP_Master](nolock) 
where EMPID=stg.EMPID and hashValue=stg.hashvalue )  order by EMPID; 

这给出了 1000 的重复值。有人可以帮我吗?谢谢先生

4

1 回答 1

0

基本上它是一个简单的选择行,在另一个表中没有匹配的条目。你可以试试这个:

 insert into EMP_MASTER (EMPID,hashValue)
    SELECT t1.EMPID, t1.hashValue
    FROM staging t1
        LEFT JOIN master t2 ON t1.EMPID = t2.EMPID AND t1.hashValue = t2.hashValue
    WHERE t2.EMPID IS NULL 
于 2013-11-09T13:50:59.490 回答