Oracle - 如何使用多个外部连接编写查询并连接到 ansi sql 中的日期
Oracle - How to write a query with multiple outer joins and joins to dates in ansi sql
我正在尝试使用外连接在 ansi (sql 92) sql 中编写以下查询:
select *
from ota_delegate_bookings odb,
ota_events oe, -- aka Class
per_all_assignments_f paaf
where oe.event_id = odb.event_id
and paaf.person_id (+) = odb.delegate_person_id
and paaf.assignment_type (+) in ('E', 'C')
and paaf.primary_flag (+) = 'Y'
and oe.course_start_date between paaf.effective_start_date (+) and paaf.effective_end_date (+)
我遇到了可怕的 ORA-01417: a table may be outer joined to at most one other table
错误。我想知道较新的 ansi sql 外连接语法是否没有外连接到多个表的限制;但我不知道如何将其写成 ansi sql。您如何处理多个连接以及一个连接如何使用 between?
您有右联接和左联接的组合,这就是导致 table 最多与另一个 table 外部联接的错误的原因。 table per_all_assignments_f
外连接到 ota_events
和 ota_delegate_bookings
。我不知道这个限制是否也适用于 ANSI SQL 连接;我的建议是使用子查询或 CTE(我不确定连接是否有意义)。
(还有,我要问一下,你真的需要SELECT *
吗?)
WITH ota AS (
SELECT * FROM ota_delegate_bookings odb
INNER JOIN ota_events oe
ON odb.event_id = oe.event_id
)
SELECT * FROM ota LEFT JOIN per_all_assignments_f paaf
ON ota.delegate_person_id = paaf.person_id
AND ota.course_start_date BETWEEN paaf.effective_start_date AND paaf.effective_end_date
AND paaf.assignment_type IN ('E', 'C')
AND paaf.primary_flag = 'Y';
这也可以使用子查询来编写:
SELECT * FROM (
SELECT * FROM ota_delegate_bookings odb
INNER JOIN ota_events oe
ON odb.event_id = oe.event_id
) ota LEFT JOIN per_all_assignments_f paaf
ON ota.delegate_person_id = paaf.person_id
AND ota.course_start_date BETWEEN paaf.effective_start_date AND paaf.effective_end_date
AND paaf.assignment_type IN ('E', 'C')
AND paaf.primary_flag = 'Y';
我正在尝试使用外连接在 ansi (sql 92) sql 中编写以下查询:
select *
from ota_delegate_bookings odb,
ota_events oe, -- aka Class
per_all_assignments_f paaf
where oe.event_id = odb.event_id
and paaf.person_id (+) = odb.delegate_person_id
and paaf.assignment_type (+) in ('E', 'C')
and paaf.primary_flag (+) = 'Y'
and oe.course_start_date between paaf.effective_start_date (+) and paaf.effective_end_date (+)
我遇到了可怕的 ORA-01417: a table may be outer joined to at most one other table
错误。我想知道较新的 ansi sql 外连接语法是否没有外连接到多个表的限制;但我不知道如何将其写成 ansi sql。您如何处理多个连接以及一个连接如何使用 between?
您有右联接和左联接的组合,这就是导致 table 最多与另一个 table 外部联接的错误的原因。 table per_all_assignments_f
外连接到 ota_events
和 ota_delegate_bookings
。我不知道这个限制是否也适用于 ANSI SQL 连接;我的建议是使用子查询或 CTE(我不确定连接是否有意义)。
(还有,我要问一下,你真的需要SELECT *
吗?)
WITH ota AS (
SELECT * FROM ota_delegate_bookings odb
INNER JOIN ota_events oe
ON odb.event_id = oe.event_id
)
SELECT * FROM ota LEFT JOIN per_all_assignments_f paaf
ON ota.delegate_person_id = paaf.person_id
AND ota.course_start_date BETWEEN paaf.effective_start_date AND paaf.effective_end_date
AND paaf.assignment_type IN ('E', 'C')
AND paaf.primary_flag = 'Y';
这也可以使用子查询来编写:
SELECT * FROM (
SELECT * FROM ota_delegate_bookings odb
INNER JOIN ota_events oe
ON odb.event_id = oe.event_id
) ota LEFT JOIN per_all_assignments_f paaf
ON ota.delegate_person_id = paaf.person_id
AND ota.course_start_date BETWEEN paaf.effective_start_date AND paaf.effective_end_date
AND paaf.assignment_type IN ('E', 'C')
AND paaf.primary_flag = 'Y';