【问题标题】:Elegant way of doing table sync reports in Oracle?在 Oracle 中进行表同步报告的优雅方式?
【发布时间】:2010-11-12 13:58:08
【问题描述】:

所以我有两张桌子

TABLE_A

KEYA | VALUE
1    |  1.2
2    |  2.3
3    |  8.3

TABLE_B

KEYB | VALUE
1    |  1.2
3    |  1.6
4    |  5.5

我想创建这两个表之间的同步状态报告。 IE。为每个记录以下信息:

  1. 记录 X 在 TABLE_A 中,但不在 TABLE_B 中
  2. 记录 X 在 TABLE_B 中,但不在 TABLE_A 中
  3. 记录 X 存在于两个表中,但 VALUE 不同
  4. (已同步的记录不会出现在报告中)

我真的不需要文本(实际上,我不想要它)。可能只是值本身:

[Table_A.KEYA (if present)]|[Table_A.Value (if present)]|[Table_B.KEYB (if present)]|[Table_B.Value (if present)]

给定的示例表应该产生:

|2|2.3| |   |
|3|8.3|3|1.6|
| |   |4|5.5|

我目前正在使用长系列的连接和减号来执行此操作,但我认为这对于 DB 来说应该很常见,而 Oracle 可能有一种更优雅(并且可能更有效)的方式来做这件事。有没有人可以拍一些tips?

谢谢!

f.

【问题讨论】:

    标签: oracle join intersection


    【解决方案1】:
    select a.keya, a.value a_value, b.keyb, b.value b_value
    from table_a a
    full outer join table_b b
    on a.keya = b.keyb
    where a.keya IS NULL
    or b.keya IS NULL
    or (a.value is null and b.value is not null)
    or (a.value is not null and b.value is null)
    or a.value <> b.value
    

    【讨论】:

      【解决方案2】:

      我认为加入和减去都很好:-)

      上面的例子会给出类似的东西

      select *, null, null
      from a
      where not exists (select keyb
        from b 
        where keyb = a.keya)
      union all
      select a.*, b.*
      from a, b
      where a.keya = b.keya
      and a.value <> b.value -- please extend if null values are allowed
      union all
      select null, null, *
      from b
      where not exists (select keya
        from a
        where keya = b.keyb)
      

      它很快就会因为很多列而变得混乱。

      【讨论】:

        猜你喜欢
        • 2012-01-09
        • 1970-01-01
        • 2013-04-23
        • 1970-01-01
        • 1970-01-01
        • 2010-10-26
        • 1970-01-01
        • 1970-01-01
        • 2021-11-10
        相关资源
        最近更新 更多