October 29, 2012 at 4:14 pm
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.
October 29, 2012 at 4:18 pm
As 2 LEFT JOINs to the same ZipCodes table.
October 29, 2012 at 4:26 pm
I'm confused, a LEFT JOIN returns all the rows from the table, which I don't want..
October 29, 2012 at 4:40 pm
I think this might work:
INNER JOIN ZipCodes Z ON (D.MainZip = Z.Zip OR D.AlternateZip = Z.Zip)
October 29, 2012 at 4:41 pm
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) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
October 29, 2012 at 4:44 pm
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?
October 29, 2012 at 5:32 pm
Ok thank you all, I will digest this! I appreciate it.
November 1, 2012 at 12:04 am
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 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
November 1, 2012 at 2:16 am
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