DISTINCT Row in a CTE

I have a CTE trying to select only distinct Contacts that are related to a contact in tProject. But it will repeat the contact for every project they are in. Can someone please help get the contact only once please.
With ContactCTE (ID, sFirstName, sLastName, sBusinessName, sPhone, sMobile, sEmail, sStreet, sStreetNo, IsBuilder, SuburbID)
AS (
SELECT Distinct ID, sFirstName, sLastName, sBusinessName, sPhone, sMobile, sEmail, sStreet, sStreetNo, IsBuilder, SuburbID
FROM tContacts
)
SELECT    ContactCTE.ID,  ContactCTE.sFirstName + ' ' + ContactCTE.sLastName AS sName, ContactCTE.sBusinessName, ContactCTE.sPhone, ContactCTE.sMobile, ContactCTE.sEmail,
                         Isnull(tSuburbs.sSuburb, '') as sSuburb, ISNULL(tSuburbs.sPostcode, '') as sPostcode, ContactCTE.sStreetNo + ' ' + ContactCTE.sStreet AS sAddress, ContactCTE.IsBuilder,
                         ContactCTE.ID
FROM            ContactCTE INNER JOIN
                         tProject ON ContactCTE.ID = tProject.AgentID LEFT OUTER JOIN
                         tSuburbs ON ContactCTE.SuburbID = tSuburbs.ID
                         WHERE ContactCTE.IsBuilder = 1

Open in new window

mgordon-spiAsked:
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.

Kevin CrossChief Technology OfficerCommented:
Hi.

It does not appear you are using tProject in the SELECT at all. Is it's function just to ensure that the contact has at least one project?

If so, then I would try something like this (I removed all the parts that are not changing):
With ContactCTE (/*...*/)
AS (
/*...*/
)
SELECT /*...*/
FROM ContactCTE
LEFT OUTER JOIN tSuburbs ON ContactCTE.SuburbID = tSuburbs.ID
WHERE ContactCTE.IsBuilder = 1
AND EXISTS (
    SELECT 1
    FROM tProject
    WHERE ContactCTE.ID = tProject.AgentID
)
;


Hope that helps!
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
mgordon-spiAuthor Commented:
That was my first CTE and confused me completely. But that seemed to do the trick, thank you.
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.

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.