简体   繁体   中英

Oracle - How to write a query with multiple outer joins and joins to dates in ansi sql

I am trying to write the following query in ansi (sql 92) sql with outer joins:

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 (+)

I get the dreaded ORA-01417: a table may be outer joined to at most one other table error. I would like to find out whether the newer ansi sql outer join syntax does not have the limitation with outer joining to multiple tables; but I don't know how to write it as ansi sql. How do you handle more than one joins and how does one join using between?

You have a combination of right join and left join, that is what is causing the error about a table's being outer joined to at most one other table. The table per_all_assignments_f is outer joined to both ota_events and ota_delegate_bookings . I don't know if this restriction also holds in ANSI SQL joins; my advice would be to use a subquery or CTE (I'm not sure the joins even make sense otherwise).

(Also, I have to ask, do you really need 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';

This could also be written using a subquery:

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';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM