Home » SQL & PL/SQL » SQL & PL/SQL » outer join for 3 tables
outer join for 3 tables [message #155165] Thu, 12 January 2006 12:15 Go to next message
oraanu
Messages: 22
Registered: September 2005
Location: Boston
Junior Member
Hi,

Is it possible to join 3 tables using left outer join

example.. tables A,B,C
A outer join B
B outer Join C


Thanks,
Anu
Re: outer join for 3 tables [message #155169 is a reply to message #155165] Thu, 12 January 2006 12:32 Go to previous messageGo to next message
Art Metzer
Messages: 2477
Registered: December 2002
Senior Member
Yes.
SQL> CREATE TABLE a (a_id INT)
  2  /
 
Table created.
 
SQL> CREATE TABLE b (b_id INT, a_id INT)
  2  /
 
Table created.
 
SQL> CREATE TABLE c (c_id INT, b_id INT)
  2  /
 
Table created.
 
SQL> INSERT INTO a VALUES (1);
SQL> INSERT INTO a VALUES (2);
SQL> INSERT INTO a VALUES (3);
SQL> INSERT INTO b VALUES (165, 1);
SQL> INSERT INTO b VALUES (166, 2);
SQL> INSERT INTO c VALUES (7780, 165);
 
SQL> SET NULL [NULL]
SQL> SELECT   a.a_id
  2  ,        b.b_id
  3  ,        c.c_id
  4  FROM     a
  5  ,        b
  6  ,        c
  7  WHERE    a.a_id = b.a_id (+)
  8  AND      b.b_id = c.b_id (+)
  9  ORDER BY a.a_id
 10  /
 
      A_ID       B_ID       C_ID
---------- ---------- ----------
         1        165       7780
         2        166 [NULL]
         3 [NULL]     [NULL]
 
SQL> SELECT   a.a_id
  2  ,        b.b_id
  3  ,        c.c_id
  4  FROM     a LEFT OUTER JOIN b ON a.a_id = b.a_id
  5             LEFT OUTER JOIN c ON b.b_id = c.b_id
  6  ORDER BY a.a_id
  7  /
 
      A_ID       B_ID       C_ID
---------- ---------- ----------
         1        165       7780
         2        166 [NULL]
         3 [NULL]     [NULL]
 
SQL>
Re: outer join for 3 tables [message #155179 is a reply to message #155165] Thu, 12 January 2006 14:17 Go to previous message
oraanu
Messages: 22
Registered: September 2005
Location: Boston
Junior Member
Thanks a lot for ur quick reply.

when i use outer join on more than 2 tables i am getting an error like "a table may be outer joined to at most the other table.
i am sending my SQL and commented where i need outer join.can any one help me is there any other way i can do it.

select distinct c.PEO_ID,assoc.ASSOC_NAME region,trunc(sysdate - max(c.DATE_NOTE_START)) DAYS_WAITING,c.DATE_CT ,
decode(c.PROC_CODE, 'ZNC9', '65H', 'ZNC10', '65H','FSD', '24', 'Z9422', 'CM','Z9423', 'CM',
'H0023 HN', '65N', 'H0023 HO', '65N', 'H0023 HU', '65N',
'H2021 HNHO','65M', 'H2021 HU','65M','H2021 HY','65M') section,
initcap(cli.LAST_NAME||','||cli.FIRST_NAME) client,p.AGE,ad.TOWN_ID,initcap(ct.CITY_NAME),pr.RELATED_PEO_ID,
nvl(initcap(cm.LAST_NAME||','||cm.FIRST_NAME),'No Case Manager') casemanager,c.ORG_ID, c.ORG_LOCATION_ID,
nvl(initcap(o.ORG_NAME||' '|| ol.LOCATION_NAME),'No CM Agency') "CMagency" ,
nvl(initcap(qis.LAST_NAME||','||qis.FIRST_NAME),'No CE Contact') qisname ,qisr.related_peo_id

FROM people_x_association pxa,assoc_x_assoc_type at,association assoc,
ch_super_ct c,people_all cli,v_people p,address ad,city_town ct,people_relate pr,people_all cm,
org_x_org_staff os,organization o,org_location ol,people_relate qisr,people_all qis

WHERE assoc.ASSOC_ID=pxa.ASSOC_ID
and at.ASSOC_TYPE_ID=pxa.ASSOC_TYPE_ID
and at.ASSOC_TYPE_ID=101
and pxa.END_DATE is null
and assoc.ASSOC_NAME like '%CHILD%'
--and assoc.ASSOC_NAME=decode(:p_region,'ALL',assoc.ASSOC_NAME,:p_region)

and pxa.peo_id=c.peo_id
and c.wait is not null
and c.proc_code in ('ZNC9','ZNC10','FSD', 'Z9422','Z9423','H0023 HN','H0023 HO','H0023 HU','H2021 HNHO',
'H2021 HU','H2021 HY')
and c.CT_ID||c.PROCESS_ID||date_note_start IN

( SELECT c.CT_ID||c.PROCESS_ID||MAX(c.DATE_NOTE_START) FROM ch_super_ct c
WHERE date_note_start <= /*:p_date */ '12-jan-2006'
GROUP BY c.CT_ID,c.PROCESS_ID)
and c.PEO_ID=cli.PEO_ID

and c.PEO_ID=p.PEO_ID

and ad.PEO_ID(+)=c.PEO_ID
and ad.ADDRESS_TYPE_CODE(+)='L'
and ad.END_DATE(+) is null

and ad.TOWN_ID=ct.TOWN_ID

and c.PEO_ID=pr.PEO_ID(+)
and pr.RT_CODE(+)='CMGRT'
and pr.RELATED_PEO_ID=cm.PEO_ID --here i have to use outer join like pr.related_peo_id(+)=cm.peo_id
and pr.END_DATE(+) is null

and os.PEO_ID(+)=pr.RELATED_PEO_ID

and o.ORG_ID(+)=os.ORG_ID
and ol.ORG_LOCATION_ID(+)=os.ORG_LOCATION_ID
and os.end_date(+) is null

and qisr.peo_id(+)=c.PEO_ID
and qisr.RT_CODE(+)='QIS'
and qisr.END_DATE(+) is null
and qisr.RELATED_PEO_ID=qis.PEO_ID --here also i have to use outer join like qisr.related_peo_id(+)=qis.peo_id

GROUP BY c.PEO_ID,assoc.assoc_name,c.DATE_CT,c.proc_code,
initcap(cli.LAST_NAME||','||cli.FIRST_NAME),p.age,ad.TOWN_ID,ct.CITY_NAME,pr.RELATED_PEO_ID,
nvl(initcap(cm.LAST_NAME||','||cm.FIRST_NAME),'No Case Manager'),c.ORG_ID, c.ORG_LOCATION_ID,
nvl(initcap(o.ORG_NAME||' '|| ol.LOCATION_NAME),'No CM Agency'),nvl(initcap(qis.LAST_NAME||','||qis.FIRST_NAME),'No CE Contact'),
qisr.related_peo_id
order by assoc.assoc_name,client,section


Thanks,
Anu
Previous Topic:UTL_FILE handling ISO Latin 1
Next Topic:dbms_trace?
Goto Forum:
  


Current Time: Sat Nov 7 14:48:40 CST 2009

Total time taken to generate the page: 0.45845 seconds
.:: Forum Home :: Blogger Home :: Wiki Home :: Contact :: Privacy ::.