在 Oracle 中执行表同步报告的优雅方法?

发布于 2024-10-01 14:28:53 字数 824 浏览 1 评论 0原文

因此,我有两个表

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

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

  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|

我目前正在使用长系列的连接和减法来做到这一点,但我认为这对于数据库来说应该很常见,并且 Oracle 可能有一个更优雅的(并且可能更多)有效)的做法。有人可以拍摄一些提示吗?

谢谢一百万!

f.

So I have my two tables

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

And I want to create a report of the sync status between these two tables. I.e. Generate for each record the following information:

  1. Record X is in TABLE_A but not in TABLE_B
  2. Record X is in TABLE_B but not in TABLE_A
  3. Record X is present in both tables but differs in VALUE
  4. (records synced won't appear in the report)

I don't really need the text (actually, I don't want it). Could be just the values itself:

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

The given example tables should yield:

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

I'm currently doing that with longs series of joins and minuses all around, but figured that should be quite common for DBs and Oracle possibly have a more elegant (and possibly more efficient) way of doing it. Could anyone shoot some tips?

Thanks a mil!

f.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞美少年 2024-10-08 14:28:53
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
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
风启觞 2024-10-08 14:28:53

我认为连接和减法都很好:-)

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

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)

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

I think joins and minuses are fine :-)

The above example would give something like

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)

It soon gets messy with lots of columns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文