数据迁移 - 验证加载的主键可以更改的数据

Data Migration - Verify Data loaded where Primary Key can change

我目前正在尝试编写 SQL 来验证已从一个应用程序迁移到另一个应用程序的数据计数。

正在迁移的主要 table 之一有时包含目标应用程序中已存在的主键,因此需要对其进行更改。这导致我的计数不匹配。

我有这些更改后的主键的参考 table,但我不确定如何将此参考 table 合并到我的左连接中。

我真的不知道如何包含 Table A 中的密钥可能是 Table B 上的密钥或存储在 Reference table 中的新密钥的条件?

select count(*)
from table_b b
  left join table_a a on
            b.key = a.key
  where a.key is null;

引用table真的很简单,两列,old_number,new_number。它只会包含 table A 中的密钥在加载到 table B 之前需要更改的条目。

old_number, new_number
12345678, 13345678
23456781, 24456781

如何包含此场景?

select count(*)
from table_b b
  left join table_a a on
            b.key = (a.key or new_number if it exists)
  where a.key is null;

因此,如果查询可以在引用 table 中包含 new_number,那么迁移计数应该与 Table A 中的计数相匹配。

这应该有效

select count() from table_b b, table_a a where b.key = a.key UNION select count() from table_b b, reference_table re where b.key = re.new_number;