SQL 语句 - 查询一组具有最近日期的记录

发布于 2024-10-15 13:30:06 字数 4302 浏览 2 评论 0原文

我正在 Oracle 9 数据库中编写一个视图,但我很困惑。我想要解决的是:A_WPD_ADDRESS_HISTORY 包含一个名为 WPD_PLAN_CHECK_S 的 FK,并且该视图保存该 FK 的多个地址。我正在尝试获取每个 WPD_PLAN_CHECK_S (FK) 的最新地址。您将使用 FIRST_INSERTED 列,它是首次插入行的日期/时间,并对其调用聚合函数 MAX(),但这就是我目前所知道的,因此我被难住了。

以下是我认为理解这个问题需要的两个主要观点。其他表/视图与我当前正在尝试解决的问题无关。

下面:我正在尝试检索最新地址记录的主视图。

SELECT
  awa.wpd_address_history_s,
  wpc.wpd_plan_check_s,
  wpc.pcis_display,
  awa.street_num,
  awa.street_frac,
  awa.street_prefix,
  awa.street_name,
  awa.street_type_l_s,
  awa.street_suffix,
  awa.street_unit_l_s,
  awa.street_unit_num,
  awa.cross_streets,
  awa.city,
  awa.state,
  awa.state_l_s,
  awa.zip,
  awa.zip_ext,
  awa.zip_with_ext,
  wf.ind_id,
  wpc.wpd_status_l_s,
  wsl.wpd_status_desc,
  awa.location as LOCATION, 
  wc.contact_name     
FROM
  wpd_plan_check wpc
  LEFT OUTER JOIN a_wpd_address_history awa ON wpc.wpd_plan_check_s = awa.wpd_plan_check_s
  LEFT OUTER JOIN wpd_contact  wc   ON wpc.wpd_plan_check_s = wc.wpd_plan_check_s
  LEFT OUTER JOIN wpd_facility wf   ON wpc.wpd_plan_check_s = wf.wpd_plan_check_s
  LEFT OUTER JOIN wpd_status_l wsl  ON wpc.wpd_status_l_s = wsl.wpd_status_l_s
  LEFT OUTER JOIN street_type_l stl ON awa.street_type_l_s = stl.street_type_l_s
  LEFT OUTER JOIN street_unit_l sul ON awa.street_unit_l_s = sul.street_unit_l_s

--

下面:我从中提取的地址历史记录视图

   CREATE OR REPLACE FORCE VIEW a_wpd_address_history (wpd_address_history_s,
                                                        wpd_plan_check_s,
                                                        street_num,
                                                        street_frac,
                                                        street_prefix,
                                                        street_name,
                                                        street_type_l_s,
                                                        street_suffix,
                                                        street_unit_l_s,
                                                        street_unit_num,
                                                        cross_streets,
                                                        city,
                                                        state_l_s,
                                                        state,
                                                        zip,
                                                        zip_ext,
                                                        zip_with_ext,
                                                        LOCATION,
                                                        apn_num,
                                                        river_desc,
                                                        first_inserted
                                                       )
    AS
       SELECT DISTINCT a.wpd_address_history_s, a.wpd_plan_check_s, a.street_num,
                       a.street_frac, a.street_prefix, a.street_name,
                       a.street_type_l_s, a.street_suffix, a.street_unit_l_s,
                       a.street_unit_num, a.cross_streets, a.city, a.state_l_s,
                       s.state, a.zip, a.zip_ext,
                          a.zip
                       || NVL2 (a.zip_ext, '-' || a.zip_ext, '') AS zip_with_ext,
                          LTRIM (a.street_num || ' ')
                       || LTRIM (NVL (a.street_frac, ' ') || ' ')
                       || LTRIM (NVL (a.street_prefix, ' ') || ' ')
                       || LTRIM (NVL (a.street_name, ' ') || ' ')
                       || LTRIM (NVL (stl.street_type_desc, ' ') || ' ')
                       || LTRIM (NVL (a.street_suffix, ' ') || ' ') AS LOCATION,
                       a.apn_num, r.river_desc, first_inserted
                  FROM wpd_address_history a LEFT OUTER JOIN street_type_l stl
                       ON a.street_type_l_s = stl.street_type_l_s
                       LEFT OUTER JOIN street_unit_l sul
                       ON a.street_unit_l_s = sul.street_unit_l_s
                       LEFT OUTER JOIN state_l s ON a.state_l_s = s.state_l_s
                       LEFT OUTER JOIN river_l r ON a.river_l_s = r.river_l_s

I'm writing a View in Oracle 9 database, and I'm stumped. What I'm trying to solve is this: A_WPD_ADDRESS_HISTORY contains a FK called WPD_PLAN_CHECK_S, and this view holds multiple addresses for this one FK. I'm trying to get the most recent address per WPD_PLAN_CHECK_S (FK). You would use the FIRST_INSERTED column, which is the date/time that row was first inserted and call the aggregate function MAX() on it, but that's all I know at this moment, and thus I'm stumped.

Below are the two main views I think are needed to understand this problem. The other tables/views are irrelevant to this problem for what I"m currently trying to solve.

Below: Main View that I'm trying to retrieve the most recent address record on.

SELECT
  awa.wpd_address_history_s,
  wpc.wpd_plan_check_s,
  wpc.pcis_display,
  awa.street_num,
  awa.street_frac,
  awa.street_prefix,
  awa.street_name,
  awa.street_type_l_s,
  awa.street_suffix,
  awa.street_unit_l_s,
  awa.street_unit_num,
  awa.cross_streets,
  awa.city,
  awa.state,
  awa.state_l_s,
  awa.zip,
  awa.zip_ext,
  awa.zip_with_ext,
  wf.ind_id,
  wpc.wpd_status_l_s,
  wsl.wpd_status_desc,
  awa.location as LOCATION, 
  wc.contact_name     
FROM
  wpd_plan_check wpc
  LEFT OUTER JOIN a_wpd_address_history awa ON wpc.wpd_plan_check_s = awa.wpd_plan_check_s
  LEFT OUTER JOIN wpd_contact  wc   ON wpc.wpd_plan_check_s = wc.wpd_plan_check_s
  LEFT OUTER JOIN wpd_facility wf   ON wpc.wpd_plan_check_s = wf.wpd_plan_check_s
  LEFT OUTER JOIN wpd_status_l wsl  ON wpc.wpd_status_l_s = wsl.wpd_status_l_s
  LEFT OUTER JOIN street_type_l stl ON awa.street_type_l_s = stl.street_type_l_s
  LEFT OUTER JOIN street_unit_l sul ON awa.street_unit_l_s = sul.street_unit_l_s

--

Below: Address History view that I"m pulling from

   CREATE OR REPLACE FORCE VIEW a_wpd_address_history (wpd_address_history_s,
                                                        wpd_plan_check_s,
                                                        street_num,
                                                        street_frac,
                                                        street_prefix,
                                                        street_name,
                                                        street_type_l_s,
                                                        street_suffix,
                                                        street_unit_l_s,
                                                        street_unit_num,
                                                        cross_streets,
                                                        city,
                                                        state_l_s,
                                                        state,
                                                        zip,
                                                        zip_ext,
                                                        zip_with_ext,
                                                        LOCATION,
                                                        apn_num,
                                                        river_desc,
                                                        first_inserted
                                                       )
    AS
       SELECT DISTINCT a.wpd_address_history_s, a.wpd_plan_check_s, a.street_num,
                       a.street_frac, a.street_prefix, a.street_name,
                       a.street_type_l_s, a.street_suffix, a.street_unit_l_s,
                       a.street_unit_num, a.cross_streets, a.city, a.state_l_s,
                       s.state, a.zip, a.zip_ext,
                          a.zip
                       || NVL2 (a.zip_ext, '-' || a.zip_ext, '') AS zip_with_ext,
                          LTRIM (a.street_num || ' ')
                       || LTRIM (NVL (a.street_frac, ' ') || ' ')
                       || LTRIM (NVL (a.street_prefix, ' ') || ' ')
                       || LTRIM (NVL (a.street_name, ' ') || ' ')
                       || LTRIM (NVL (stl.street_type_desc, ' ') || ' ')
                       || LTRIM (NVL (a.street_suffix, ' ') || ' ') AS LOCATION,
                       a.apn_num, r.river_desc, first_inserted
                  FROM wpd_address_history a LEFT OUTER JOIN street_type_l stl
                       ON a.street_type_l_s = stl.street_type_l_s
                       LEFT OUTER JOIN street_unit_l sul
                       ON a.street_unit_l_s = sul.street_unit_l_s
                       LEFT OUTER JOIN state_l s ON a.state_l_s = s.state_l_s
                       LEFT OUTER JOIN river_l r ON a.river_l_s = r.river_l_s

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

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

发布评论

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

评论(2

稳稳的幸福 2024-10-22 13:30:06

最常见的解决方案是这样的

SELECT
  awa.wpd_address_history_s,
  wpc.wpd_plan_check_s,
  wpc.pcis_display,
  awa.street_num,
  awa.street_frac,
  awa.street_prefix,
  awa.street_name,
  awa.street_type_l_s,
  awa.street_suffix,
  awa.street_unit_l_s,
  awa.street_unit_num,
  awa.cross_streets,
  awa.city,
  awa.state,
  awa.state_l_s,
  awa.zip,
  awa.zip_ext,
  awa.zip_with_ext,
  wf.ind_id,
  wpc.wpd_status_l_s,
  wsl.wpd_status_desc,
  awa.location as LOCATION, 
  wc.contact_name     
FROM
  wpd_plan_check wpc
  LEFT OUTER JOIN a_wpd_address_history awa ON wpc.wpd_plan_check_s = awa.wpd_plan_check_s
  LEFT OUTER JOIN wpd_contact  wc   ON wpc.wpd_plan_check_s = wc.wpd_plan_check_s
  LEFT OUTER JOIN wpd_facility wf   ON wpc.wpd_plan_check_s = wf.wpd_plan_check_s
  LEFT OUTER JOIN wpd_status_l wsl  ON wpc.wpd_status_l_s = wsl.wpd_status_l_s
  LEFT OUTER JOIN street_type_l stl ON awa.street_type_l_s = stl.street_type_l_s
  LEFT OUTER JOIN street_unit_l sul ON awa.street_unit_l_s = sul.street_unit_l_s
WHERE awa.first_inserted = (SELECT MAX(awa2.first_inserted)
                             FROM a_wpd_address_history awa2
                            WHERE awa.wpd_plan_check_s = awa2.wpd_plan_check_s)

,但是使用分析函数执行类似的操作可能会更有效,这样您只需点击视图一次。

WITH awa AS (
  SELECT awa_inner.*, 
         RANK() OVER (PARTITION BY wpd_plan_check_s ORDER BY first_inserted DESC) rnk
    FROM a_wpd_address_history awa_inner
)
SELECT
  awa.wpd_address_history_s,
  wpc.wpd_plan_check_s,
  wpc.pcis_display,
  awa.street_num,
  awa.street_frac,
  awa.street_prefix,
  awa.street_name,
  awa.street_type_l_s,
  awa.street_suffix,
  awa.street_unit_l_s,
  awa.street_unit_num,
  awa.cross_streets,
  awa.city,
  awa.state,
  awa.state_l_s,
  awa.zip,
  awa.zip_ext,
  awa.zip_with_ext,
  wf.ind_id,
  wpc.wpd_status_l_s,
  wsl.wpd_status_desc,
  awa.location as LOCATION, 
  wc.contact_name     
FROM
  wpd_plan_check wpc
  LEFT OUTER JOIN awa               ON wpc.wpd_plan_check_s = awa.wpd_plan_check_s
  LEFT OUTER JOIN wpd_contact  wc   ON wpc.wpd_plan_check_s = wc.wpd_plan_check_s
  LEFT OUTER JOIN wpd_facility wf   ON wpc.wpd_plan_check_s = wf.wpd_plan_check_s
  LEFT OUTER JOIN wpd_status_l wsl  ON wpc.wpd_status_l_s = wsl.wpd_status_l_s
  LEFT OUTER JOIN street_type_l stl ON awa.street_type_l_s = stl.street_type_l_s
  LEFT OUTER JOIN street_unit_l sul ON awa.street_unit_l_s = sul.street_unit_l_s
WHERE awa.rnk = 1

The most common solution would be something like

SELECT
  awa.wpd_address_history_s,
  wpc.wpd_plan_check_s,
  wpc.pcis_display,
  awa.street_num,
  awa.street_frac,
  awa.street_prefix,
  awa.street_name,
  awa.street_type_l_s,
  awa.street_suffix,
  awa.street_unit_l_s,
  awa.street_unit_num,
  awa.cross_streets,
  awa.city,
  awa.state,
  awa.state_l_s,
  awa.zip,
  awa.zip_ext,
  awa.zip_with_ext,
  wf.ind_id,
  wpc.wpd_status_l_s,
  wsl.wpd_status_desc,
  awa.location as LOCATION, 
  wc.contact_name     
FROM
  wpd_plan_check wpc
  LEFT OUTER JOIN a_wpd_address_history awa ON wpc.wpd_plan_check_s = awa.wpd_plan_check_s
  LEFT OUTER JOIN wpd_contact  wc   ON wpc.wpd_plan_check_s = wc.wpd_plan_check_s
  LEFT OUTER JOIN wpd_facility wf   ON wpc.wpd_plan_check_s = wf.wpd_plan_check_s
  LEFT OUTER JOIN wpd_status_l wsl  ON wpc.wpd_status_l_s = wsl.wpd_status_l_s
  LEFT OUTER JOIN street_type_l stl ON awa.street_type_l_s = stl.street_type_l_s
  LEFT OUTER JOIN street_unit_l sul ON awa.street_unit_l_s = sul.street_unit_l_s
WHERE awa.first_inserted = (SELECT MAX(awa2.first_inserted)
                             FROM a_wpd_address_history awa2
                            WHERE awa.wpd_plan_check_s = awa2.wpd_plan_check_s)

However it's likely more efficient to do something like this with analytic functions so that you only have to hit the view once.

WITH awa AS (
  SELECT awa_inner.*, 
         RANK() OVER (PARTITION BY wpd_plan_check_s ORDER BY first_inserted DESC) rnk
    FROM a_wpd_address_history awa_inner
)
SELECT
  awa.wpd_address_history_s,
  wpc.wpd_plan_check_s,
  wpc.pcis_display,
  awa.street_num,
  awa.street_frac,
  awa.street_prefix,
  awa.street_name,
  awa.street_type_l_s,
  awa.street_suffix,
  awa.street_unit_l_s,
  awa.street_unit_num,
  awa.cross_streets,
  awa.city,
  awa.state,
  awa.state_l_s,
  awa.zip,
  awa.zip_ext,
  awa.zip_with_ext,
  wf.ind_id,
  wpc.wpd_status_l_s,
  wsl.wpd_status_desc,
  awa.location as LOCATION, 
  wc.contact_name     
FROM
  wpd_plan_check wpc
  LEFT OUTER JOIN awa               ON wpc.wpd_plan_check_s = awa.wpd_plan_check_s
  LEFT OUTER JOIN wpd_contact  wc   ON wpc.wpd_plan_check_s = wc.wpd_plan_check_s
  LEFT OUTER JOIN wpd_facility wf   ON wpc.wpd_plan_check_s = wf.wpd_plan_check_s
  LEFT OUTER JOIN wpd_status_l wsl  ON wpc.wpd_status_l_s = wsl.wpd_status_l_s
  LEFT OUTER JOIN street_type_l stl ON awa.street_type_l_s = stl.street_type_l_s
  LEFT OUTER JOIN street_unit_l sul ON awa.street_unit_l_s = sul.street_unit_l_s
WHERE awa.rnk = 1
暖风昔人 2024-10-22 13:30:06

有很多 SQL 需要消化。

在示例表中尝试类似的操作:

desc foo;
key1    integer
key2    integer
bar_date date
foo_text varchar(255)


select a.*
from foo a
where a.bar_date = (
  select max(b.bar_date)
  from foo b
  where b.key1 = a.key1
    and b.key2 = a.key2
);

That's a lot of SQL to digest.

Try something like this in an example table:

desc foo;
key1    integer
key2    integer
bar_date date
foo_text varchar(255)


select a.*
from foo a
where a.bar_date = (
  select max(b.bar_date)
  from foo b
  where b.key1 = a.key1
    and b.key2 = a.key2
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文