通过仅选择其中 1 行并跳过其余行来解决具有重复主键的 2 个表的并集

Resolve union of 2 tables that have duplicate primary key by picking only 1 of the rows and skip the rest of the rows

我有 2 个 table,我正在使用以下查询加入他们

Select distinct EmailAddress,CUSTOMER_ID,Send_Date,Unique_key,sub_category
from table1

UNION ALL

Select distinct EmailAddress,CUSTOMER_ID,Send_Date,Unique_key,sub_category
from table2

我正在使用 Unique_key 作为主键。它是发送日期 + 客户 ID 的组合。有时两个 table 都可以有重复的键,因此在这种情况下我只想使用上面的查询

获取 1row
Table 1                 
EmailAddress       CUSTOMER_ID  Send_Date   Unique_key  sub_category
a@gmail.com        1001         07-08-2021  70820211001 chair
                
                
Table 2             
EmailAddress       CUSTOMER_ID  Send_Date   Unique_key  sub_category
a@gmail.com        1001         07-08-2021  7082021100  book
                

预期结果是什么?

EmailAddress    CUSTOMER_ID Send_Date   Unique_key  sub_category
a@gmail.com     1001        07-08-2021  70820211001 chair

只有 1 条记录应该出现在最后的 table & 应该跳过多行。我不想更改唯一密钥格式的任何内容。有什么解决方法吗?

你需要这样的东西:

Select distinct EmailAddress,CUSTOMER_ID,Send_Date,sub_category
from table_1
UNION 
SELECT EmailAddress,CUSTOMER_ID,Send_Date,sub_category FROM table_2 
WHERE NOT EXISTS ( SELECT NULL 
                   FROM table_1 
                   WHERE table_1.EmailAddress = table_2.EmailAddress ) ;

下面的select将return空集,因为你有WHERE NOT EXISTS条件,return不匹配的行。

SELECT EmailAddress,CUSTOMER_ID,Send_Date,sub_category 
FROM table_2 
WHERE NOT EXISTS ( SELECT NULL 
                   FROM table_1 
                   WHERE table_1.EmailAddress = table_2.EmailAddress 
                  ) ;

演示:https://www.db-fiddle.com/f/pB6b5xrgPKCivFWcpQHsyE/24

试试你的数据然后告诉我。