Join on Null

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

Post Reply
vsi
Premium Member
Premium Member
Posts: 507
Joined: Wed Mar 15, 2006 1:44 pm

Join on Null

Post by vsi »

I want to join two tables TableA, TableB

Code: Select all

      A.Col1=B.Col1
  and A.Col2=B.Col2
The thing is that I should allow for the join on one condition when other condition fails due to NULLSi.e. say A.Col1 and B.Col1 both are NULL.

I tried with NVL in join condition. But the statement hangs up. Whats the most efficient way
urshit_1983
Participant
Posts: 73
Joined: Wed Jun 28, 2006 3:27 pm
Location: NJ

Post by urshit_1983 »

Use

Cats(A.Col=B.Col ) = sv1
If IsNull(sv1) Then sv1 Else @NULL = sv2


Thanks
"Nobody is expert in Everything,
But Everybody is expert in Something."
Krazykoolrohit
Charter Member
Charter Member
Posts: 560
Joined: Wed Jul 13, 2005 5:36 am
Location: Ohio

Post by Krazykoolrohit »

check if A.Col1 is nul and riase a flag. similary check if B.Col1 is null and raise another flag.

Do a join only if both the flags are up.
vsi
Premium Member
Premium Member
Posts: 507
Joined: Wed Mar 15, 2006 1:44 pm

Post by vsi »

how can i achieve that purely on SQL
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

You are looking for a SQL solution, I assume, and not a DataStage one. You haven't specified a database - Oracle?
-craig

"You can never have too many knives" -- Logan Nine Fingers
urshit_1983
Participant
Posts: 73
Joined: Wed Jun 28, 2006 3:27 pm
Location: NJ

Post by urshit_1983 »

Ok then in input stage of Database select the option

Generate Select statement and enter other clauses.

There just give the join statement

where A.columnname=B.columnname is Null
"Nobody is expert in Everything,
But Everybody is expert in Something."
DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

Post by DSguru2B »

look into the Collasce function. You can use that in the where predicates. I might be off by a word or two in the name of the function. I have used that before in my sql queries. What it basicaly does is checks if the key is null, it will replace it with a default value that you specify. The format is
Collasce(ColA, DefaultValue). Look into it, that might help.
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
vsi
Premium Member
Premium Member
Posts: 507
Joined: Wed Mar 15, 2006 1:44 pm

Post by vsi »

a) Is coalesce more efficient than nvl()
b) As its not necessary to join when both columns of same table (i.e. A.Col1 and A.Col2) are NULL, then is Case(i) more efficient or Case (ii)

Case (i)

Code: Select all

select * 
from TableA A

inner join Table B B 
on coalesce(A.Col1,'')=coalesce(B.Col2,'')
and coalesce(A.Col2,'')=coalesce(B.Col2,'')

Case (ii)

Code: Select all

select * 
from TableA A

inner join Table B B 
on coalesce(A.Col1,'')=coalesce(B.Col2,'')
and coalesce(A.Col2,'')=coalesce(B.Col2,'')

where (coalesce(A.Col1,0) and coalesce(A.Col2,0)) =1 
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

urshit_1983 wrote:Cats(A.Col=B.Col ) = sv1
If IsNull(sv1) Then sv1 Else @NULL = sv2
It is not permissible to assign to the @NULL system variable as you have done. I suspect you are trying to indicate "assign to". Maybe ==> would have been a better symbol.

What are you trying to achieve with the Cats() function? As you have written it, it can only ever return 1 or 0, and is not required. The expression A.Col=B.Col will return exactly the same result.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Post Reply