Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

SQL Join between two tables two columns, but the data in the join condition could be null.

799664Nov 25 2013 — edited Nov 26 2013

hello all,

can anyone help me write an outer join b/n the two tables below. The joining condition has if's and or's.

table 1 has 2 million rows, table 2 is very small

TABLE1
CUSTOMER_IDCITYSTATE
1SKOKIEIL
2CHICAGOIL
3CARYNC
.ERIEPA
.PHILLYPA
CHARLOTENC
.
.
.
2 MILLIONCITYXYSTATEX

TABLE2
CITYSTATECONTACT
ILOJO
ERIE BRITT
PAMIKE
PITTSBURGPAHILTON
N043TATaffi
Baffi
Rb0b
Qb0b
CHARLOTENCb0b

problem :: for all the data in table1, I need to find out the CONTACT from table 2 And the join condition would be as below

1. either both TABLE1.CITY=TABLE2.CITY AND TABLE1.STATE=TABLE2.STATE and get CONTACT
OR
2. TABLE1.CITY=TABLE2.CITY AND TABLE2.STATE IS NULLĀ  and get the value of CONTACT
OR
3. TABLE1.STATE=TABLE2.STATE AND TABLE2.CITY is null and get the value of CONTACT

I need a query like this

SELECT A.CUSTOMER_ID, A.CITY, A.STATE, B.CONTACT
FROM TABLE1 A, TABLE2 B
WHERE (join condition fitting in the 3 condition mentioned above)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 24 2013
Added on Nov 25 2013
14 comments
1,525 views