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

时间:2011-02-02 02:55:07

标签: sql oracle oracle9i

我正在Oracle 9数据库中编写一个View,我很难过。我要解决的是: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

2 个答案:

答案 0 :(得分:3)

这是要消化的很多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
);

答案 1 :(得分:3)

最常见的解决方案是

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