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

  • 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.

  • As 2 LEFT JOINs to the same ZipCodes table.


    Alex Suprun

  • I'm confused, a LEFT JOIN returns all the rows from the table, which I don't want..

  • I think this might work:

    INNER JOIN ZipCodes Z ON (D.MainZip = Z.Zip OR D.AlternateZip = Z.Zip)

  • 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) A socialist is someone who will give you the shirt off *someone else's* back.

  • 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

  • Ok thank you all, I will digest this! I appreciate it.

  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Thank you Dwain!

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply