﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / Some way to write an INNER JOIN, but joining on either of two columns / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 02:44:08 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Some way to write an INNER JOIN, but joining on either of two columns</title><link>http://www.sqlservercentral.com/Forums/Topic1378503-392-1.aspx</link><description>Thank you Dwain!</description><pubDate>Thu, 01 Nov 2012 02:16:34 GMT</pubDate><dc:creator>matt6749</dc:creator></item><item><title>RE: Some way to write an INNER JOIN, but joining on either of two columns</title><link>http://www.sqlservercentral.com/Forums/Topic1378503-392-1.aspx</link><description>[quote][b]matt6749 (10/29/2012)[/b][hr]Hi,Each doctor in my doctors table has a main zip code and an alternate zip code.SELECT D.* FROM Doctors DINNER JOIN ZipCodes Z ON D.MainZip = Z.ZipCodeOR D.AlternateZip = Z.ZipCodeI've been trying, but can't figure out how to do this (the OR part).. thanks.[/quote]Your query can probably be made to work.  Try this:[code="sql"]DECLARE @Doctors TABLE (name VARCHAR(10), MainZip VARCHAR(5), AlternateZip VARCHAR(5))INSERT INTO @DoctorsSELECT 'Dr. Dwain', '12345', NULLUNION 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 @ZipCodesSELECT '12345' UNION ALL SELECT '12346' UNION ALL SELECT '12347'-- Returns 2 results of Dr. ChrisSELECT D.* FROM @Doctors DINNER JOIN @ZipCodes Z ON D.MainZip = Z.ZipCodeOR D.AlternateZip = Z.ZipCode[/code]If the issue is dups being returned, add DISTINCT to your SELECT.Otherwise, look at CELKO's method.  That should work too.</description><pubDate>Thu, 01 Nov 2012 00:04:07 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Some way to write an INNER JOIN, but joining on either of two columns</title><link>http://www.sqlservercentral.com/Forums/Topic1378503-392-1.aspx</link><description>Ok thank you all, I will digest this!  I appreciate it.</description><pubDate>Mon, 29 Oct 2012 17:32:20 GMT</pubDate><dc:creator>matt6749</dc:creator></item><item><title>RE: Some way to write an INNER JOIN, but joining on either of two columns</title><link>http://www.sqlservercentral.com/Forums/Topic1378503-392-1.aspx</link><description>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?</description><pubDate>Mon, 29 Oct 2012 16:44:07 GMT</pubDate><dc:creator>David Webb-CDS</dc:creator></item><item><title>RE: Some way to write an INNER JOIN, but joining on either of two columns</title><link>http://www.sqlservercentral.com/Forums/Topic1378503-392-1.aspx</link><description>The ZIP codes are attributes of the Doctors addresses. There should not be a JOIN at all!CREATE TABLE Doctors(doctor_license_nbr CHAR(12) NOT NULL PRIMARY KEY, doctor_first_name VARCHAR(20) NOT NULL, doctor_first_name VARCHAR(20) NOT NULL, main_zip_code CHAR(5) NOT NULL    CHECK (main_zip_code LIKE '[0-9][0-9][0-9][0-9][0-9]'), alternate_zip_code CHAR(5) --- nullable?    CHECK (alternate_zip_code LIKE '[0-9][0-9][0-9][0-9][0-9]'), CHECK (main_zip_code &amp;lt;&amp;gt; alternate_zip_code),  ..);I am guessing at the specs you did not post. There are more validations, but I think you get the point. If you want to a look up against a particular list in a table:SELECT D.*  FROM Doctors AS D, Z AS Special_ZIP_codes WHERE D.main_zip_code        IN (SELECT zip_code FROM Special_ZIP_codes)    OR D.alternate_zip_code        IN (SELECT zip_code FROM Special_ZIP_codes) </description><pubDate>Mon, 29 Oct 2012 16:42:37 GMT</pubDate><dc:creator>CELKO</dc:creator></item><item><title>RE: Some way to write an INNER JOIN, but joining on either of two columns</title><link>http://www.sqlservercentral.com/Forums/Topic1378503-392-1.aspx</link><description>What is the problem?  Do you have NULLs in the the MainZip when it's not present/used?[code="sql"]SELECT D.* FROM Doctors DINNER JOIN ZipCodes Z ON    (D.MainZip IS NOT NULL AND D.MainZip = Z.ZipCode) OR     (D.MainZip IS NULL AND D.AlternateZip = Z.ZipCode)[/code]</description><pubDate>Mon, 29 Oct 2012 16:41:48 GMT</pubDate><dc:creator>ScottPletcher</dc:creator></item><item><title>RE: Some way to write an INNER JOIN, but joining on either of two columns</title><link>http://www.sqlservercentral.com/Forums/Topic1378503-392-1.aspx</link><description>I think this might work:INNER JOIN ZipCodes Z ON (D.MainZip = Z.Zip OR D.AlternateZip = Z.Zip)</description><pubDate>Mon, 29 Oct 2012 16:40:13 GMT</pubDate><dc:creator>matt6749</dc:creator></item><item><title>RE: Some way to write an INNER JOIN, but joining on either of two columns</title><link>http://www.sqlservercentral.com/Forums/Topic1378503-392-1.aspx</link><description>I'm confused, a LEFT JOIN returns all the rows from the table, which I don't want..</description><pubDate>Mon, 29 Oct 2012 16:26:11 GMT</pubDate><dc:creator>matt6749</dc:creator></item><item><title>RE: Some way to write an INNER JOIN, but joining on either of two columns</title><link>http://www.sqlservercentral.com/Forums/Topic1378503-392-1.aspx</link><description>As 2 LEFT JOINs to the same ZipCodes table.</description><pubDate>Mon, 29 Oct 2012 16:18:03 GMT</pubDate><dc:creator>Alexander Suprun</dc:creator></item><item><title>Some way to write an INNER JOIN, but joining on either of two columns</title><link>http://www.sqlservercentral.com/Forums/Topic1378503-392-1.aspx</link><description>Hi,Each doctor in my doctors table has a main zip code and an alternate zip code.SELECT D.* FROM Doctors DINNER JOIN ZipCodes Z ON D.MainZip = Z.ZipCodeOR D.AlternateZip = Z.ZipCodeI've been trying, but can't figure out how to do this (the OR part).. thanks.</description><pubDate>Mon, 29 Oct 2012 16:14:44 GMT</pubDate><dc:creator>matt6749</dc:creator></item></channel></rss>