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
The most common solution would be something like
However it's likely more efficient to do something like this with analytic functions so that you only have to hit the view once.