如何比较两个配置单元 table 记录

How to compare two hive table records

我有两个 tables Core 和 BKP。 Table BKP 包含的数据 duplicates.But Core 包含没有重复的相同数据。

两个 table 的主键是 4 个字段 (1,2,3,4) 的组合。

但是在 运行 一些脚本之后,Core table 中的一些记录丢失了。

如何从 Core table 中找出遗漏的记录,(遗漏的记录在 BKP 中肯定存在,但有重复)。

使用 NOT EXISTS:

Select b.PK1,b.PK2,b.PK3,b.PK4 --Primary key from BKP missed in CORE
 from 
(--select all PK existing in BKP
  select 
      distinct
      b.PK1,b.PK2,b.PK3,b.PK4 --Primary key from BKP
  from BKP b) b
where not exists(select 1 from CORE c 
                  where c.PK1=b.PK1 
                    and c.PK2=b.PK2
                    and c.PK3=b.PK3
                    and c.PK4=b.PK4
                )

使用左连接:

    Select b.PK1,b.PK2,b.PK3,b.PK4 --Primary key from BKP missed in CORE
     from 
    (--select all PK existing in BKP
      select 
          distinct
          b.PK1,b.PK2,b.PK3,b.PK4 --Primary key from BKP
      from BKP b
    ) b
     left join 
               (select c.PK1,c.PK2,c.PK3,c.PK4 --Primary key from CORE
                  from CORE c 
                ) c 
           on c.PK1=b.PK1 
              and c.PK2=b.PK2
              and c.PK3=b.PK3
              and c.PK4=b.PK4
  where c.PK1 is null --absent in CORE