使用另一个表的最大日期更新一个表

发布于 2024-08-26 01:02:47 字数 322 浏览 7 评论 0原文

在 Oracle 10g 中,我需要使用表 B 中的数据更新表 A。

表 A 具有 LOCATION、TRAANDATE 和 STATUS。

表 B 具有 LOCATION、STATUSDATE 和 STATUS

我需要使用表 B 中的 STATUS 列更新表 A 中的 STATUS 列,其中 STATUSDATE 是截至并包括该 LOCATION 的 TRANDATE 的最大日期(基本上,我正在获取状态特定交易时的位置)。

我有一个 PL/SQL 过程可以做到这一点,但我知道必须有一种方法让它使用分析来工作,而且我已经绞尽脑汁太久了。

谢谢!

In Oracle 10g, I need to update Table A with data from Table B.

Table A has LOCATION, TRANDATE, and STATUS.

Table B has LOCATION, STATUSDATE, and STATUS

I need to update the STATUS column in Table A with the STATUS column from Table B where the STATUSDATE is the max date upto and including the TRANDATE for that LOCATION (basically, I'm getting the status of the location at the time of a particular transaction).

I have a PL/SQL procedure that will do this but I KNOW there must be a way to get it to work using an analytic, and I've been banging my head too long.

Thanks!

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

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

发布评论

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

评论(2

何以畏孤独 2024-09-02 01:02:47

这应该可以帮助您开始(这里的 MAX 函数是聚合函数,而不是分析函数):

UPDATE table_a
   SET status = (SELECT MAX(table_b.status) 
                        KEEP (DENSE_RANK FIRST ORDER BY table_b.statusdate DESC)
                   FROM table_b
                  WHERE table_a.location = table_b.location
                    AND table_b.statusdate <= table_a.trandate);

这将更新 table_a 中的所有行,即使 table_b< 中没有先前的行/code>,在这种情况下将状态更新为 NULL。如果您只想更新 table_a 中与 table_b 中具有相应匹配项的行,则可以添加过滤器:

UPDATE table_a
   SET status = (SELECT MAX(table_b.status) 
                        KEEP (DENSE_RANK FIRST ORDER BY table_b.statusdate DESC)
                   FROM table_b
                  WHERE table_a.location = table_b.location
                    AND table_b.statusdate <= table_a.trandate)
 WHERE EXISTS (SELECT NULL
                 FROM table_b
                WHERE table_a.location = table_b.location
                  AND table_b.statusdate <= table_a.trandate);

this should get you started (Here the MAX function is the aggregate function and not the analytic function):

UPDATE table_a
   SET status = (SELECT MAX(table_b.status) 
                        KEEP (DENSE_RANK FIRST ORDER BY table_b.statusdate DESC)
                   FROM table_b
                  WHERE table_a.location = table_b.location
                    AND table_b.statusdate <= table_a.trandate);

This will update all the rows in table_a, even if there is no prior row in table_b, updating the status to NULL in that case. If you only want to update the rows in table_a that have a corresponding match in table_b you can add a filter:

UPDATE table_a
   SET status = (SELECT MAX(table_b.status) 
                        KEEP (DENSE_RANK FIRST ORDER BY table_b.statusdate DESC)
                   FROM table_b
                  WHERE table_a.location = table_b.location
                    AND table_b.statusdate <= table_a.trandate)
 WHERE EXISTS (SELECT NULL
                 FROM table_b
                WHERE table_a.location = table_b.location
                  AND table_b.statusdate <= table_a.trandate);
禾厶谷欠 2024-09-02 01:02:47

这是一个带有解析功能的版本。它更新 table_a 中的所有行,如图所示。要更新特定行,请添加过滤器。

update table_a t1 set status = (
       select distinct
              first_value(t2.status) over (partition by t1.location, t1.trandate order by t2.statusdate desc)
       from temp_b t2 
       where t1.location = t2.location
       and t2.statusdate <= t1.trandate );

This is a version with the analytic function. It update all the rows in table_a as shown. To update a specific row, add a filter.

update table_a t1 set status = (
       select distinct
              first_value(t2.status) over (partition by t1.location, t1.trandate order by t2.statusdate desc)
       from temp_b t2 
       where t1.location = t2.location
       and t2.statusdate <= t1.trandate );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文