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