Subquery has multiple values to compare to in the Where clause

I have a database table that contains race result information.

Table: Results
Name      Track      RaceDate      EventType      RaceNumber
Banana      PA      2008-01-01       A      1
Orange      PA      2008-01-01       A      1
Blue      PA      2008-01-01       A      1
Purple      OK      2008-02-01       A      2
Lemon      MA      2008-02-01       A      2
Peanut      PA      2008-02-01      A      2
Blue      PA      2008-02-01      A      2
+250,000 more

My user selects a random assortment of races that we need to analyze.  All of the choices are inserted into a 'temporary' database table for later use.

Table: TemporaryRaceResults
Track      RaceDate      EventType      RaceNumber
PA      2008-01-01      A      1
PA      2008-01-01      A      2
PA      2008-02-01      A      1
PA      2008-02-01      A      5
+ 20 or so more

When the SQL script is run, I want to return an unique list of names of participants in the races.

Although it doesn't work too badly, I don't want to use a cursor if I can help it.
One reason is that it doesn't properly return a unique list of names, but rather is full of duplicates.
Is there a way in T-SQL to return just what I want?

DECLARE @track char(2)
DECLARE @raceDate smalldatetime
DECLARE @eventType char(1)
DECLARE @raceNumber tinyint

DECLARE MY_CURSOR Cursor FAST_FORWARD FOR
SELECT Track, RaceDate, EventType, RaceNumber FROM TemporaryRaceResults;

OPEN MY_CURSOR

FETCH NEXT FROM MY_CURSOR INTO @track, @raceDate, @eventType, @raceNumber

WHILE @@FETCH_STATUS = 0
BEGIN

      SELECT DISTINCT Name FROM Results
      WHERE Results.Track = @track
        AND Results.RaceDate = @raceDate
        AND Results.EventType = @eventType
        AND Results.RaceNumber = @raceNumber

      FETCH NEXT FROM MY_CURSOR INTO @track, @raceDate, @eventType, @raceNumber
END

CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR

Thanks for your time!
LVL 7
psdavisAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kevin CrossChief Technology OfficerCommented:
You can use a simple INNER JOIN.
SELECT DISTINCT r.Name
FROM Results r
INNER JOIN TemporaryRaceResults t
ON t.track = r.track AND t.racedate = r.racedate
AND t.eventtype = r.eventtype AND t.racenumber = r.racenumber

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
psdavisAuthor Commented:
Ok, I just wAyayyyyyyy overthought this one.

Thanks for the quick answer mwvisa1!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.