【问题标题】:Compare tables from different databases with the same data but different number of columns比较不同数据库中具有相同数据但列数不同的表
【发布时间】:2016-02-12 19:55:43
【问题描述】:

在 Oracle SQL Developer 中,如何比较 A + B = C 表的三个表?我必须验证是否将 A 和 B 的所有数据都转换为 C。此外,表 A 与 B 和 C 位于不同的数据库中,它们位于同一数据库中。

【问题讨论】:

    标签: sql oracle oracle11g oracle-sqldeveloper oracle12c


    【解决方案1】:

    让我假设不同的数据库有一个列,id。您可以为此使用full outer join,假设它永远不是NULL。但是,使用union all 和聚合可能更容易。

    您可以使用以下查询获取不同的 id 列表:

    select id, sum(inab) as inab, sum(inc) as inc
    from ((select id, 1 as inab, 0 as inc
           from a
          ) union all
          (select id, 1 as inab, 0 as inc
           from b
          ) union all
          (select id, 0 as inab, 1 as inc
           from c
          ) 
         ) c
    group by id
    having sum(inab) <> 1 or sum(inc) <> 1;
    

    实际上,您可能会有多个列。注意:如果 A+B 或 C 中有重复,这只是保证重复出现在两者中(而不是在两者中具有相同的计数)。

    【讨论】:

    • inab 是什么意思? inc 是什么意思? B 和 C 中的列数相同,但 A 中的列数不同。那么 UNION ALL 在这里如何工作
    • 这些是列别名,是 SQL 语言的重要组成部分,UNION ALL 也是如此。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 2021-12-16
    • 1970-01-01
    • 2013-05-18
    • 2020-07-24
    • 1970-01-01
    相关资源
    最近更新 更多