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
 General SQL Server Forums
 New to SQL Server Programming
 Duplicate records

Author  Topic 

dr223
Constraint Violating Yak Guru

444 Posts

Posted - 2010-08-27 : 09:44:06

Hi,

I have the following code which when I execute gives duplicate records. I tried to use the Distinct command but still it took over 1 minute to yield results, thought it yields the right results..Why is it taking such a long long time.. When I remove the Distinct command it takes 3 seconds and yields duplicate records.. Any help please

SELECT Distinct  TOP (100) PERCENT dbo.tblProjects.ISACProtocol, dbo.tblProjectScheduleEv.EvTypeID, dbo.tblProjectScheduleEv.EvDate, 
dbo.tblProjectScheduleEv.EvBFDate, dbo.tblProjects.CompletionDt, dbo.tblProjectScheduleEv.RemLetterGenDate, dbo.tblProjects.ProjectDesc,
dbo.qrycrtspsheetall.prac_no, dbo.qrycrtspsheetall.main_doctor, dbo.qrycrtspsheetall.address1, dbo.qrycrtspsheetall.address2,
dbo.qrycrtspsheetall.address3, dbo.qrycrtspsheetall.town, dbo.qrycrtspsheetall.county, dbo.qrycrtspsheetall.post_code
FROM dbo.tblCurrProj INNER JOIN
dbo.qrycrtspsheetall ON dbo.tblCurrProj.Curr_ProjectID = dbo.qrycrtspsheetall.ProjectID INNER JOIN
dbo.tblProjectScheduleEv INNER JOIN
dbo.tblProjectPractices ON dbo.tblProjectScheduleEv.ProjectPracticeID = dbo.tblProjectPractices.ProjectPracticeID INNER JOIN
dbo.tblProjects ON dbo.tblProjectPractices.ProjectID = dbo.tblProjects.ProjectID INNER JOIN
dbo.tblProjectPatients ON dbo.tblProjects.ProjectID = dbo.tblProjectPatients.ProjectID INNER JOIN
dbo.qryMaxEvTypeID ON dbo.tblProjects.ProjectID = dbo.qryMaxEvTypeID.ProjectID AND
dbo.tblProjectScheduleEv.EvTypeID = dbo.qryMaxEvTypeID.MaxEvTypeID ON dbo.qrycrtspsheetall.ProjectID = dbo.tblProjects.ProjectID AND
dbo.qrycrtspsheetall.prac_no = dbo.tblProjectPractices.Prac_no AND dbo.qrycrtspsheetall.prac_no = dbo.tblProjectPatients.Prac_no
WHERE (dbo.tblProjects.ProjectStatus = 'True') AND (dbo.tblProjectPatients.No = 'true')

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2010-08-27 : 14:15:12
without knowing your data its hard to provide a suggestion.. I would start with commenting out all the joins except the 2nd table.. and keep uncommenting one join at a time to see which join is retrieving duplicate results.. perhaps you missed another join condition somewhere?

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-28 : 01:37:59
that means you do have a one to many relationship existing between any of tables involved in query. Try applying group by on table based on columns you want and then take join to avoid duplicates rather than using distinct at the end

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

dr223
Constraint Violating Yak Guru

444 Posts

Posted - 2010-08-31 : 05:49:23
visakh16 how do you that ? Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-09-01 : 11:35:51
which table has one to many relationship in above case?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -