5

I've got a scenario where I want to switch on two different tables in an outer join. It goes something like this:-

         select mytable.id, 
                yourtable.id
           from mytable
left outer join (case
                    when mytable.id = 2 then table2 
                      yourtable on table1.id = table2.id
                    else
                      table3 yourtable on table1.id = table3.id
                 end)

...but it doesn't work. Any suggestions?

6
  • 2
    It might be worth re-examining your database design. If both table2 and table3 have the same or similar schemas, why are they different tables? Mar 22, 2010 at 5:44
  • It is also worth re-examining your example. There seems to be alias/tablename mix-up.
    – jva
    Mar 22, 2010 at 12:08
  • Just join to BOTH tables and move CASE part to column list
    – user38123
    Mar 22, 2010 at 19:52
  • @oleksiy Yes joining works but then I get values in two different columns, not in one.
    – s khan
    Mar 25, 2010 at 3:59
  • @Cohen The tables are similar, but not about same stuff. So have to keep different. Its like I have 2 tables for,staff and students, referring to one table, faculty, that then further refers to two more tables etc. I first had to deal with the former case i.e., get the faculties for staff and students while keeping one column for staff and student. Anyways, thanks to all.
    – s khan
    Mar 25, 2010 at 4:07

3 Answers 3

5

Use (Oracle 9i+):

   SELECT mt.id, 
          COALESCE(yt1.id, yt2.id)
     FROM MYTABLE mt
LEFT JOIN YOURTABLE yt1 ON yt1.id = mt.id
                       AND yt.id = 2
LEFT JOIN YOURTABLE yt2 ON yt2.id = mt.id
3
  • 1
    For rows where mt.id is 2 but there are multiple matches in yt2 for that id, this could generate duplicates.
    – Gary Myers
    Mar 22, 2010 at 22:10
  • @Gary: True, I thought about it when posting but decided to post a query that matched the OP verbatim.
    – OMG Ponies
    Mar 23, 2010 at 1:13
  • Thanks. this worked out fine and no, there aren't supposed to be any duplicate values so this handled that as well.
    – s khan
    Mar 25, 2010 at 3:57
3

Here's another possibility, although I haven't tried it on Oracle:

select mytable.id,  
       yourtable.id 
from table1 as mytable left outer join 
    (SELECT 2 AS tableid, *
     FROM table2
     UNION ALL
     SELECT 1, *
     FROM table3) as yourtable
    ON mytable.id = yourtable.id
    AND tableid = CASE WHEN mytable.id = 2 THEN 2 ELSE 1 END
1

This query joins records from the EMP table to either the DEPT table or the SPECIAL_OPS table, depending on the value of EMP.DEPTNO ...

SQL> select e.ename
  2         , e.job
  3         , e.deptno
  4         , coalesce(d.dname, s.dname) as dname
  5  from  emp e
  6        left outer join dept d
  7             on ( e.deptno = 30
  8                  and e.deptno = d.deptno )
  9        left outer join special_ops s
 10             on ( e.deptno != 30
 11                  and e.deptno = s.deptno )
 12  where e.deptno in (30,50)
 13  order by e.deptno, e.empno
 14  /

ENAME      JOB           DEPTNO DNAME
---------- --------- ---------- --------------
VAN WIJK   SALESMAN          30 SALES
PADFIELD   SALESMAN          30 SALES
BILLINGTON SALESMAN          30 SALES
SPENCER    MANAGER           30 SALES
CAVE       SALESMAN          30 SALES
HALL       CLERK             30 SALES
VERREYNNE  PLUMBER           50 SKUNKWORKS
FEUERSTEIN PLUMBER           50 SKUNKWORKS

8 rows selected.

SQL>

I have included the filter on EMP.DEPTNO in the ON clauses. This might be unnecessary if the data in the tables is exclusive (i.e. DEPTNO = 30 could only join to DEPT and DEPTNO = 50 could only join to SPECIAL_OPS). However, if the identifier can appear in both tables it is as well to be explicit. Besides, making our intent clear is always good practice. Apart from anything else, we cannot be sure about the future state of the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.