Oracle - 两个表中两行之间的差异或变化

发布于 2024-08-09 02:21:46 字数 147 浏览 4 评论 0原文

我有两个表都有相同的架构,一个表有前一天的记录,另一个表有当前的记录。我想比较两者并仅查找更改或仅在至少一列中具有不同值的行。

这在pl/sql、oracle中怎么可能呢? (我在 T-SQL 中使用校验和编写了类似的代码,但不确定如何在 pl/sql 中执行)

I have two tables both have same schema, one will have previous day records other will have current. I want to compare both and find only changes or only rows that have different value in atleast one column.

How is this possible in pl/sql, oracle?
(I did code something similar using checksum in T-SQL but not sure how to do in pl/sql)

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

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

发布评论

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

评论(3

×纯※雪 2024-08-16 02:21:46

这个SO答案提供了一个非常有效的解决方案,使用Oracle来比较2个查询的结果:证明SQL查询等价性

This SO answer provides a very efficient solution using Oracle to compare the results of 2 queries: Proving SQL query equivalency

琴流音 2024-08-16 02:21:46

正如 Dougman 发布的 之一比较两个数据集的最有效方法是将它们分组。我通常使用这样的查询来比较两个表:

SELECT MIN(which), COUNT(*), pk, col1, col2, col3, col4
  FROM (SELECT 'old data' which, pk, col1, col2, col3, col4
           FROM t
         UNION ALL
         SELECT 'new data' which, pk, col1, col2, col3, col4 FROM t_new)
 GROUP BY pk, col1, col2, col3, col4
HAVING COUNT(*) != 2
 ORDER BY pk, MIN(which);

As Dougman posted one of the most efficient way to compare two data sets is to GROUP them. I usually use a query like this to compare two tables:

SELECT MIN(which), COUNT(*), pk, col1, col2, col3, col4
  FROM (SELECT 'old data' which, pk, col1, col2, col3, col4
           FROM t
         UNION ALL
         SELECT 'new data' which, pk, col1, col2, col3, col4 FROM t_new)
 GROUP BY pk, col1, col2, col3, col4
HAVING COUNT(*) != 2
 ORDER BY pk, MIN(which);
云淡月浅 2024-08-16 02:21:46

您需要一个独占差异查询,它本质上是一个 (AB) U (BA) 查询:

(SELECT * FROM TODAY_TABLE
MINUS
SELECT * FROM YESTERDAY_TABLE)
UNION ALL
(SELECT * FROM YESTERDAY_TABLE
MINUS
SELECT * FROM TODAY_TABLE)

还可以轻松改进此查询,以使用附加计算列显示哪些记录是插入、删除和更改。

You want an exclusive-difference query, which is is essentially an (A-B) U (B-A) query:

(SELECT * FROM TODAY_TABLE
MINUS
SELECT * FROM YESTERDAY_TABLE)
UNION ALL
(SELECT * FROM YESTERDAY_TABLE
MINUS
SELECT * FROM TODAY_TABLE)

This query can also be easily improved to also show which records are inserts, deletes, and changes using an additional calculated column.

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