识别两个表之间差异的最快方法?

发布于 2024-08-21 17:41:19 字数 291 浏览 2 评论 0原文

我需要根据事务存档表检查实时表,但我不确定执行此操作的最快方法...

例如,假设我的实时表由以下列组成:

  • Term
  • CRN
  • Fee
  • Level Code

My存档表将具有相同的列,但也有存档日期,这样我就可以看到活动表在给定日期有哪些值。

现在...我将如何编写查询来确保活动表的值与存档表中的最新条目相同?

PS 我更喜欢在 SQL 中处理这个问题,但如果速度更快,PL/SQL 也是一个选择。

I have a need to check a live table against a transactional archive table and I'm unsure of the fastest way to do this...

For instance, let's say my live table is made up of these columns:

  • Term
  • CRN
  • Fee
  • Level Code

My archive table would have the same columns, but also have an archive date so I can see what values the live table had at a given date.

Now... How would I write a query to ensure that the values for the live table are the same as the most recent entries in the archive table?

PS I'd prefer to handle this in SQL, but PL/SQL is also an option if it's faster.

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

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

发布评论

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

评论(5

沐歌 2024-08-28 17:41:19
SELECT term, crn, fee, level_code
FROM live_data
MINUS
SELECT term, crn, fee, level_code
FROM historical_data

直播中有哪些内容,但历史中没有。然后可以结合到相反的方向来获得历史中但不是生活中的内容。

SELECT term, crn, fee, level_code
FROM live_data
MINUS
SELECT term, crn, fee, level_code
FROM historical_data

Whats on live but not in historical. Can then union to a reverse of this to get whats in historical but not live.

避讳 2024-08-28 17:41:19

简单地:

SELECT collist
  FROM TABLE A
minus 
SELECT collist
  FROM TABLE B
UNION ALL
SELECT collist
  FROM TABLE B
minus 
SELECT collist
  FROM TABLE A;

Simply:

SELECT collist
  FROM TABLE A
minus 
SELECT collist
  FROM TABLE B
UNION ALL
SELECT collist
  FROM TABLE B
minus 
SELECT collist
  FROM TABLE A;
无声无音无过去 2024-08-28 17:41:19

您没有提到如何唯一标识行,所以我假设您还有一个“id”列:

SELECT *
FROM livetable
WHERE (term, crn, fee, levelcode) NOT IN (
   SELECT FIRST_VALUE(term) OVER (ORDER BY archivedate DESC)
         ,FIRST_VALUE(crn) OVER (ORDER BY archivedate DESC)
         ,FIRST_VALUE(fee) OVER (ORDER BY archivedate DESC)
         ,FIRST_VALUE(levelcode) OVER (ORDER BY archivedate DESC)
   FROM   archivetable
   WHERE  livetable.id = archivetable.id
);

注意:此查询不考虑 NULL - 如果任何列可以为空,您可以添加合适的逻辑(例如,NVL 的每一列都为某个“不可能”的值)。

You didn't mention how rows are uniquely identified, so I've assumed you also have an "id" column:

SELECT *
FROM livetable
WHERE (term, crn, fee, levelcode) NOT IN (
   SELECT FIRST_VALUE(term) OVER (ORDER BY archivedate DESC)
         ,FIRST_VALUE(crn) OVER (ORDER BY archivedate DESC)
         ,FIRST_VALUE(fee) OVER (ORDER BY archivedate DESC)
         ,FIRST_VALUE(levelcode) OVER (ORDER BY archivedate DESC)
   FROM   archivetable
   WHERE  livetable.id = archivetable.id
);

Note: This query doesn't take NULLS into account - if any of the columns are nullable you can add suitable logic (e.g. NVL each column to some "impossible" value).

月寒剑心 2024-08-28 17:41:19

卸载到 table.unl
从表1中选择*
order by 1,2,3,4

卸载到table2.unl
从表2中选择*
order by 1,2,3,4

diff table1.unl table2.unl >差异unl

unload to table.unl
select * from table1
order by 1,2,3,4

unload to table2.unl
select * from table2
order by 1,2,3,4

diff table1.unl table2.unl > diff.unl

谁的新欢旧爱 2024-08-28 17:41:19

您是否可以使用以下形式的查询:

SELECT your columns FROM your live table
EXCEPT
SELECT your columns FROM your archive table WHERE archive date is most recent;

任何结果都将是实时表中不位于最新存档中的行。

如果您还需要最新存档中不在实时表中的行,只需反转选择的顺序并重复,或者通过执行 (实时 UNION 存档)将它们全部包含在同一个查询中,除非 (实时交集存档)

Could you use a query of the form:

SELECT your columns FROM your live table
EXCEPT
SELECT your columns FROM your archive table WHERE archive date is most recent;

Any results will be rows in your live table that are not in your most recent archive.

If you also need rows in your most recent archive that are not in your live table, simply reverse the order of the selects, and repeat, or get them all in the same query by performing a (live UNION archive) EXCEPT (live INTERSECTION archive)

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