我有两张表Customer_staging
和Customer
。 customer_staging表包含字段customer_num
,birth_date
和birth_month
,此表每天都会刷新。此表中的任何新附加记录都应更新到实际的Customer
表中。此外,任何与birth_date
和birth_month
的值不匹配的记录都应更新到Customer
表中。
我尝试了以下查询。
Select customer_num, birth_date, birth_month from Customer_Staging
EXCEPT
Select customer_num, birth_date, birth_month from Customer
INNER JOIN Customer_Staging ON Customer_Staging.customer_num = Customer.customer_num
我得到了我想要的结果,但是,我得到了更多的记录。我在查询的第二部分中添加了内部联接,认为结果集将限制在具有较少记录数的表中。 Customer_Staging表有3170条记录,其中Customer表目前只有40条记录。例如,如果我有5条记录在两者之间匹配,则结果集有3165条记录。但是,我想获得仅存在于Customer表中的记录的差异结果集。
有人可以帮忙吗?
答案 0 :(得分:0)
嵌套选择:
Select DISTINCT customer_num, birth_date, birth_month from Customer_Staging
WHERE customer_num NOT IN (Select customer_num
From Customer)