Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 query problem

Author  Topic 

johnmaio
Starting Member

9 Posts

Posted - 2008-05-27 : 17:08:14
I know I am doing something totally stupid but I can not figure this out....

here is my query

SELECT dbo.WVisit.StudyID, dbo.WVisit.Eligible
FROM dbo.WVisit INNER JOIN dbo.WPARTICIPANTS ON dbo.WVisit.StudyID = dbo.WPARTICIPANTS.STUDYID


and here is the results


9210 99
7777 99
9210 99
7777 99

why am I getting duplicate results

????

the results should be

9210 99
7777 99

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-05-27 : 17:16:19
Add distinct after Select.
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-05-27 : 17:19:27
>>Add distinct after Select.
that would work but don't hide the problem...

I'm guessing you have 2 participants with associated with each studyid.
Since you aren't selecting anything from wparticipants can you remove it from the query?

Be One with the Optimizer
TG
Go to Top of Page

johnmaio
Starting Member

9 Posts

Posted - 2008-05-27 : 17:40:57
quote:
Originally posted by TG

>>Add distinct after Select.
that would work but don't hide the problem...

I'm guessing you have 2 participants with associated with each studyid.
Since you aren't selecting anything from wparticipants can you remove it from the query?

Be One with the Optimizer
TG




I am using info from wparticipants and I still get duplicates...DISTINCT works but i don't understand why i get dups
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-05-27 : 20:19:15
either:
you have dupes in one or both of the tables
or you are missing correlation column(s) in your join criteria.

Post the structure of those table including the Primary Key (or unique constraint)

or you can figure it out yourself by looking at each table individually. You will probably be able to see why:
SELECT * FROM wparticipants where studyid = 9210
SELECT * FROM Eligible where studyid = 9210


Be One with the Optimizer
TG
Go to Top of Page

johnmaio
Starting Member

9 Posts

Posted - 2008-05-27 : 20:28:26
forget it...i am an idiot...there were multiple rows of data in each table...a trigger gone wrong put them there :(

thanks for all the help!









quote:
Originally posted by johnmaio

quote:
Originally posted by TG

>>Add distinct after Select.
that would work but don't hide the problem...

I'm guessing you have 2 participants with associated with each studyid.
Since you aren't selecting anything from wparticipants can you remove it from the query?

Be One with the Optimizer
TG




I am using info from wparticipants and I still get duplicates...DISTINCT works but i don't understand why i get dups

Go to Top of Page
   

- Advertisement -