April 11, 2011 at 7:34 am
Hi experts,
I have 2 tables where I want to join by ID1, IF NOT THEN join by ID2.
Would this code work?
select * from table1
left join table2
on coalesce(table1.ID1,table1.ID2) = coalesce(table2.ID1,table2.ID2)
I feel like I am missing something with this code, but I can't catch where. Please advise. Thank you very much for your input!
April 11, 2011 at 7:43 am
is it possible that Table1.ID1= Table2.ID2?, or that Table1.ID2= Table2.ID1?
wouldn't it be more correct to join with an OR?
select * from table1
left join table2
on (table1.ID1 = table2.ID1)
OR (table1.ID2 = table2.ID2)
Lowell
April 11, 2011 at 8:02 am
Hi Lowell,
It is possible to have ID1=ID2. Would I be sure to know I am joining by ID1 fist, and if not ID2 with your code?
Thank you so much for your help!
April 11, 2011 at 8:10 am
ichiyo85 (4/11/2011)
Hi Lowell,It is possible to have ID1=ID2. Would I be sure to know I am joining by ID1 fist, and if not ID2 with your code?
Thank you so much for your help!
well, you know your data better than i do;
I'd like to see some sample data, but so far, based on your questions....
the COALESCE returns the first non-null object in the parameter array. so you have [Table1] where either ID1 and ID2 can be null, but not both? is that right? and the same for [Table2]?;
if that's true, then your coalesce method sounds fine;
Lowell
April 11, 2011 at 8:31 am
I would reccomend using ISNULL() instead of COALESCE()
SQL Server will most often interpret COALESCE as a CASE expression which can hinder the query optimizer from generating/choosing an optimal query plan while ISNULL is a direct comparison....
While both will work just fine for the most part for smaller datasets, however, running this against several million rows of data will result in a slower running query if you use the COALESCE.
Just a thought!
______________________________________________________________________________Never argue with an idiot; Theyll drag you down to their level and beat you with experience
April 11, 2011 at 9:02 am
Thank you very much experts.
Yes I will not have both ID1 and ID2 to be null. Also, ISNULL might not be a good idea for me because ID1 is set as "Unassigned" instead of "NULL" in my database.
I think I will stick to coalesce for now. Again, I really appreciate your help!!!
April 11, 2011 at 9:13 am
ISNULL may still be preferred as ISNULL will cast the result to the datatype of the first expression evaluated.
Again, just a performance thought - hope it works out either way!
______________________________________________________________________________Never argue with an idiot; Theyll drag you down to their level and beat you with experience
April 12, 2011 at 2:19 am
Please refer the following link:
http://sqlzealot.blogspot.com/search/label/System%20Functions
April 12, 2011 at 2:57 am
ichiyo85 (4/11/2011)
Hi experts,I have 2 tables where I want to join by ID1, IF NOT THEN join by ID2.
Would this code work?
select * from table1
left join table2
on coalesce(table1.ID1,table1.ID2) = coalesce(table2.ID1,table2.ID2)
I feel like I am missing something with this code, but I can't catch where. Please advise. Thank you very much for your input!
Does the above query return the expected results? If it does, then you can almost certainly improve the performance by using OR:
SELECT *
FROM table1 t1
LEFT JOIN table2 t2
ON t2.ID1 = t1.ID1
OR t2.ID1 = t1.ID2
OR t2.ID2 = t1.ID1
OR t2.ID2 = t1.ID2
WHERE t1.ID1 IS NOT NULL AND t2.ID1 IS NOT NULL
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
April 12, 2011 at 2:59 am
ichiyo85 (4/11/2011)
Thank you very much experts.Yes I will not have both ID1 and ID2 to be null. Also, ISNULL might not be a good idea for me because ID1 is set as "Unassigned" instead of "NULL" in my database.
I think I will stick to coalesce for now. Again, I really appreciate your help!!!
"Unassigned"? Can you elaborate on this?
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
April 12, 2011 at 1:44 pm
"Unassigned" is just a name (nvarchar) that I gave to those cells that don't have values because I didn't want to leave them as NULL. Would it create any problem?
Thank you so much for all your comments!
April 12, 2011 at 1:48 pm
Also,
SELECT *
FROM table1 t1
LEFT JOIN table2 t2
ON t2.ID1 = t1.ID1
OR t2.ID1 = t1.ID2
OR t2.ID2 = t1.ID1
OR t2.ID2 = t1.ID2
WHERE t1.ID1 IS NOT NULL AND t2.ID1 IS NOT NULL
would not return what I need because I don't want to have t2.ID1 = t1.ID2 or t2.ID2 = t1.ID1 .
I want to join FIRST by t2.ID1 = t1.ID1 and if ID1 does not exist join using t2.ID2 = t1.ID2.
April 12, 2011 at 1:53 pm
ichiyo85 (4/12/2011)
"Unassigned" is just a name (nvarchar) that I gave to those cells that don't have values because I didn't want to leave them as NULL. Would it create any problem?Thank you so much for all your comments!
It won't create a problem per se, but you can't really use COALESCE or ISNULL if there are not any NULL values can you?
April 13, 2011 at 1:10 pm
Let me get this straight - if ID1 is not 'Unassigned' then you want to use it for the join, but if ID1 is 'Unassigned,' then you want to use ID2, right?
In that case, how about this:
SELECT *
FROM table1 t1
LEFT JOIN table2 t2
ON (t2.ID1 = t1.ID1 AND t1.ID1 <> 'Unassigned')
OR (t2.ID2 = t1.ID2 AND t1.ID1 = 'Unassigned')
April 13, 2011 at 2:00 pm
ichiyo85 (4/12/2011)
"Unassigned" is just a name (nvarchar) that I gave to those cells that don't have values because I didn't want to leave them as NULL. Would it create any problem?Thank you so much for all your comments!
What do you do with your integer columns? 😛
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
Viewing 15 posts - 1 through 15 (of 27 total)
You must be logged in to reply to this topic. Login to reply