Using DISTINCT SQL Query

Will this query grab all participants in one of the races in an event, but if more than one entered the same email address (as family members often do), it will only take one with that email address?
      sql = "SELECT DISTINCT p.Email, p.FirstName, p.LastName FROM Participant p INNER JOIN PartRace pr ON p.ParticipantID = pr.ParticipantID WHERE pr.RaceID IN (" & sEventRaces & ") ORDER BY p.LastName, p.FirstName"

Thank you.
Bob SchneiderCo-OwnerAsked:
Who is Participating?
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.

ste5anSenior DeveloperCommented:
Just run your query agains a set of test data, which simulates your case. What does your query return?

btw, the question is could not be correctly answered, cause we don't know whether Participant is a table or view - this could introduce multiple rows by JOINS - or if the column Email is in its table has a UNIQUE constraint or not.

We can only guess, but as already said, you should test this, otherwise you'll never get a good grip on such problems.

My guess is yes. But this requires that the tuple (Email, FirstName, LastName) is unique and for all family members different.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
it will only take one with that email address?
I would say no, unless everyone registered with the same name.
Example of rows returned for family Smith (Parents & 1 son):
p.Email, p.FirstName, p.LastName
John.Smith@gmail.com, John, Smith
John.Smith@gmail.com, Susan, Smith
John.Smith@gmail.com, William, Smith
0
Bob SchneiderCo-OwnerAuthor Commented:
Participant and PartRace are both tables that are joined in a PK-FK relationship with ParticipantID.  Using the example above with the Smith family, how could I re-write that query to only get one of the family members.  I am ultimately going to be sending informational emails and I don't want to send multiple emails.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Use TOP 1 for return only a record. In this example will be the first one ordered by the first name since the Email and Last Name should be the same:
sql = "SELECT TOP 1 p.Email, p.FirstName, p.LastName FROM Participant p INNER JOIN PartRace pr ON p.ParticipantID = pr.ParticipantID WHERE pr.RaceID IN (" & sEventRaces & ") ORDER BY p.FirstName"

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
Bob SchneiderCo-OwnerAuthor Commented:
Thanks for the help!
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

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.