SQL 语句 - 查询一组具有最近日期的记录
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最常见的解决方案是这样的
,但是使用分析函数执行类似的操作可能会更有效,这样您只需点击视图一次。
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.
有很多 SQL 需要消化。
在示例表中尝试类似的操作:
That's a lot of SQL to digest.
Try something like this in an example table: