从具有多个查找表的 Oracle Apex SQL 表中提取并展平数据

问题描述 投票:0回答:1

我正在 ServiceNow 中使用外部 Oracle Apex 数据库,目标是从主表收集数据,该主表有 21 个对不同查找表的引用。每个查找表都维护自己的“修改日期时间”列。

ServiceNow 从 JDBC 数据源加载数据时,需要源表上有一个日期时间列,它将用于执行增量查询并减少需要按计划加载的数据量。这必须是单个列,它不处理列。如果我只需要使用主表中的数据,那就没问题,但如果查找表中的数据发生了更改,它不会影响主表为该记录返回的修改日期时间。

我想查找主表上的每条记录,然后查找查找表上的每个引用,并比较每个查找表的上次修改日期时间,并从所有记录中选择最新的一条作为真正的上次修改日期时间返回对于该记录,然后对主表上选择的每个记录执行此操作。

我尝试了这个,但它似乎只是从所有引用的表中获取了最新日期,无论我从主表上的哪条记录开始

SELECT
    main_view.*,
    COALESCE(
        (SELECT MAX(update_datetime) FROM table1),
        (SELECT MAX(update_datetime) FROM table2),
        -- Add more subqueries for the remaining lookup tables...
        (SELECT MAX(update_datetime) FROM table21)
    ) AS most_recent_update_datetime
FROM
    your_existing_view main_view;

因此,不是每个返回的记录都有不同的日期时间,而是它们都返回 06-APR-2023

我可以从我正在使用的视图中提取查询并对其进行修改,但这高于我过去所做的任何 SQL 工作。

这是基于 ChatGPT 回复的指导

我相信我可以通过在 ServiceNow 数据源中禁用“使用上次运行日期时间”来解决此问题,但这违反了 JDBC 数据源的最佳实践(导致每个计划加载执行完整的数据库查询,而不是增量查询)

oracle oracle-apex etl servicenow
1个回答
0
投票

我正在 ServiceNow 中使用外部 Oracle Apex 数据库

  • 这很可能是错误的说法。没有“Oracle Apex”数据库;您指的是“XE”(快捷版)吗? Apex(“Application Express”)是一个工具,用于开发基于 Oracle 数据库的 Web 应用程序。

目标是从主表收集数据,该主表有 21 个对不同查找表的引用。每个查找表都维护自己的“修改日期时间”列。

  • 好的,所以最终结果是
    select * From main_table where ...
    ,即您不需要从 21 个查找表中的任何一个中获取数据,但您必须将主表的
    modified_datetime
    与 21 个查找表中的每一个
    modified_datetime
    值进行比较.

您发布的查询使用

coalesce
功能。在我看来,这是一个错误的选择。它接受两个(或更多)参数并返回第一个不为空的参数。例如:

SQL> select coalesce (null, null, 'A', null, 'B') result from dual;

R
-
A

假设 21 个查找表中的每一个都至少有一行,您的代码从

updated_datetime
返回 MAX
table1
;其余的被忽略。您是想使用
greatest
功能吗?它返回所有涉及参数的最大值?

SQL> select greatest
  2    (date '2023-01-15',    -- 15th of January 2023
  3     date '2023-05-18',    -- 18th of May
  4     date '2023-08-15'     -- 15th of August (today) -> that's the greatest value here
  5    ) as result
  6  from dual;

RESULT
-----------
15-Aug-2023

SQL>

这是我的理解。

样本数据(主表和3个查找表):

SQL> select * from main_table;

        ID MODIFIED_DATETIME
---------- --------------------
         1 13-Aug-2023
         2 14-Aug-2023
         3 17-Aug-2023          --> this row's modified datetime isn't largest
                                    than MAX update datetime from lookup tables,
                                    so its data shouldn't be gathered

SQL> select * from table1;

UPDATE_DATETIME
--------------------
26-Jul-2023

SQL> select * from table2;

UPDATE_DATETIME
--------------------
05-Aug-2023

SQL> select * from table3;

UPDATE_DATETIME
--------------------
15-Aug-2023            --> this is MAX update datetime in all lookup tables

查询将如下所示:

SQL> with t_greatest as
  2    (select greatest ( (select max(update_datetime) from table1),
  3                       (select max(update_datetime) from table2),
  4                       (select max(update_datetime) from table3)
  5                     ) max_lookup_datetime
  6     from dual
  7    )
  8  select m.id, m.modified_datetime,
  9    case when m.modified_datetime < g.max_lookup_datetime then 'gather data'
 10         else 'do not gather data'
 11    end as what_to_do
 12  from t_greatest g cross join main_table m;

        ID MODIFIED_DATETIME    WHAT_TO_DO
---------- -------------------- ------------------
         1 13-Aug-2023          gather data
         2 14-Aug-2023          gather data
         3 17-Aug-2023          do not gather data

SQL>

或者,您可以

greatest
所有 MAX
union
值并获取其中的 MAX,而不是
update_datetime
函数;结果是一样的,看看哪个表现更好。

SQL> with t_greatest as
  2    (select max(update_datetime) max_lookup_datetime
  3     from
  4       (select max(update_datetime) update_datetime from table1 union all
  5        select max(update_datetime)                 from table2 union all
  6        select max(update_datetime)                 from table3
  7       )
  8    )
  9  select m.id, m.modified_datetime,
 10    case when m.modified_datetime < g.max_lookup_datetime then 'gather data'
 11         else 'do not gather data'
 12    end as what_to_do
 13  from t_greatest g cross join main_table m;

        ID MODIFIED_DATETIME    WHAT_TO_DO
---------- -------------------- ------------------
         1 13-Aug-2023          gather data
         2 14-Aug-2023          gather data
         3 17-Aug-2023          do not gather data

SQL>

可能(也可能有)其他选择,但我希望这可以帮助您开始。

© www.soinside.com 2019 - 2024. All rights reserved.