I am writing a view in an Oracle 9 database and I am at a dead end. I am trying to solve this: A_WPD_ADDRESS_HISTORY contains an FK called WPD_PLAN_CHECK_S, and this view contains several addresses for this FK. I am trying to get the latest address for WPD_PLAN_CHECK_S (FK). You should use the FIRST_INSERTED column, which is the date / time when this row was inserted first, and call the aggregate function MAX () on it, but all I know at the moment, and thus I'm at a dead end .
The following are two key points that I think are needed to understand this issue. Other tables / views are not relevant to this problem for what I'm trying to solve now.
Below: the main view that I am trying to get the most recent address record.
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: view of the address history that I draw 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