Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Some way to write an INNER JOIN, but joining on either of two columns


Some way to write an INNER JOIN, but joining on either of two columns

Author
Message
matt6749
matt6749
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 216
Hi,

Each doctor in my doctors table has a main zip code and an alternate zip code.

SELECT D.*
FROM Doctors D
INNER JOIN ZipCodes Z
ON D.MainZip = Z.ZipCode
OR D.AlternateZip = Z.ZipCode

I've been trying, but can't figure out how to do this (the OR part).. thanks.
Alexander Suprun
Alexander Suprun
Old Hand
Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)

Group: General Forum Members
Points: 341 Visits: 1516
As 2 LEFT JOINs to the same ZipCodes table.


Alex Suprun
matt6749
matt6749
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 216
I'm confused, a LEFT JOIN returns all the rows from the table, which I don't want..
matt6749
matt6749
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 216
I think this might work:
INNER JOIN ZipCodes Z ON (D.MainZip = Z.Zip OR D.AlternateZip = Z.Zip)
ScottPletcher
ScottPletcher
Hall of Fame
Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)

Group: General Forum Members
Points: 3954 Visits: 6692
What is the problem? Do you have NULLs in the the MainZip when it's not present/used?



SELECT D.*
FROM Doctors D
INNER JOIN ZipCodes Z ON
(D.MainZip IS NOT NULL AND D.MainZip = Z.ZipCode) OR
(D.MainZip IS NULL AND D.AlternateZip = Z.ZipCode)



SQL DBA,SQL Server MVP('07, '08, '09)

Prosecutor James Blackburn, in closing argument in the "Fatal Vision" murders trial: "If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them."
David Webb-CDS
David Webb-CDS
SSC Eights!
SSC Eights! (922 reputation)SSC Eights! (922 reputation)SSC Eights! (922 reputation)SSC Eights! (922 reputation)SSC Eights! (922 reputation)SSC Eights! (922 reputation)SSC Eights! (922 reputation)SSC Eights! (922 reputation)

Group: General Forum Members
Points: 922 Visits: 8584
It's not clear what you're trying to accomplish. Some sample data and sample expected results would help to clear that up. If the mainzip and alternatezip both match rows in the zipcode table are you saying you just want the row that matches the mainzip returned, otherwise return whichever row matches?



And then again, I might be wrong ...
David Webb
matt6749
matt6749
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 216
Ok thank you all, I will digest this! I appreciate it.
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4287 Visits: 6431
matt6749 (10/29/2012)
Hi,

Each doctor in my doctors table has a main zip code and an alternate zip code.

SELECT D.*
FROM Doctors D
INNER JOIN ZipCodes Z
ON D.MainZip = Z.ZipCode
OR D.AlternateZip = Z.ZipCode

I've been trying, but can't figure out how to do this (the OR part).. thanks.


Your query can probably be made to work. Try this:


DECLARE @Doctors TABLE (name VARCHAR(10), MainZip VARCHAR(5), AlternateZip VARCHAR(5))

INSERT INTO @Doctors
SELECT 'Dr. Dwain', '12345', NULL
UNION ALL SELECT 'Dr. Jeff', NULL, '12346'
UNION ALL SELECT 'Dr. Chris', '12345', '12347'
UNION ALL SELECT 'Dr. Paul', '22222', '33333'

DECLARE @ZipCodes TABLE (ZipCode VARCHAR(5))

INSERT INTO @ZipCodes
SELECT '12345' UNION ALL SELECT '12346' UNION ALL SELECT '12347'

-- Returns 2 results of Dr. Chris
SELECT D.*
FROM @Doctors D
INNER JOIN @ZipCodes Z
ON D.MainZip = Z.ZipCode
OR D.AlternateZip = Z.ZipCode




If the issue is dups being returned, add DISTINCT to your SELECT.

Otherwise, look at CELKO's method. That should work too.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
matt6749
matt6749
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 216
Thank you Dwain!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search