SQL DISTINCT shows 2 same results

This SQL syntax shows 2 same results.
DISTINCT is not working.
What am I missing?

Thank you
SELECT DISTINCT C.CustomerID, C.FirstName, C.LastName,  C.Email, C.Password, C.Address, C.City, C.State, C.Country, C.Post, C.Phone, C.BestTimes, C.CreateDate, C.LastLogin, C.ContactPref, C.EstGiven, C.JobFini, C.Deposit, C.ActiveCust, C.CustNotes, B1.ImageID AS LatestImageID, B2.Comment AS LatestComment, B2.Poster, B2.PostDate AS Postdate FROM Customers AS C LEFT OUTER JOIN Employees AS E ON C.empID = E.empID LEFT OUTER JOIN (SELECT CustomerID, MAX(PostDate) AS MaxDate FROM blog WHERE (ImageID <> '') GROUP BY CustomerID) AS M1 ON M1.CustomerID = C.CustomerID LEFT OUTER JOIN blog AS B1 ON B1.CustomerID = M1.CustomerID AND B1.PostDate = M1.MaxDate LEFT OUTER JOIN (SELECT CustomerID, MAX(PostDate) AS MaxDate FROM blog AS blog_1 WHERE (Comment <> '') GROUP BY CustomerID) AS M2 ON M2.CustomerID = C.CustomerID LEFT OUTER JOIN blog AS B2 ON B2.CustomerID = M2.CustomerID AND B2.PostDate = M2.MaxDate WHERE (E.Username = @username) AND (C.ActiveCust = 1) ORDER BY C.LastLogin DESC

Open in new window

jtuttle99Asked:
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:
Are they exactly the same or is there one column that is different?

What is your SQL platform and version?
0
pssandhuCommented:
When you say 2 same results, do you mean all the columns listed in the sql are repeating or most of them but there are couple that have different value.
When you do a select distinct like above that means all the values in all the columns have to same in order to be grouped into one distinct record.
P.
0
Kevin CrossChief Technology OfficerCommented:
In the interim, you can try it like this:
SELECT C.CustomerID, C.FirstName, C.LastName,  C.Email, C.Password
, C.Address, C.City, C.State, C.Country, C.Post
, C.Phone, C.BestTimes, C.CreateDate, C.LastLogin, C.ContactPref
, C.EstGiven, C.JobFini, C.Deposit, C.ActiveCust, C.CustNotes
, B1.ImageID AS LatestImageID, B2.Comment AS LatestComment, B2.Poster, B2.PostDate AS Postdate 
FROM Customers AS C 
LEFT OUTER JOIN Employees AS E ON C.empID = E.empID 
LEFT OUTER JOIN ((SELECT CustomerID, MAX(PostDate) AS MaxDate FROM blog WHERE (ImageID <> '') GROUP BY CustomerID) AS M1
	INNER JOIN blog AS B1 ON B1.CustomerID = M1.CustomerID AND B1.PostDate = M1.MaxDate) ON M1.CustomerID = C.CustomerID 
LEFT OUTER JOIN ((SELECT CustomerID, MAX(PostDate) AS MaxDate FROM blog AS blog_1 WHERE (Comment <> '') GROUP BY CustomerID) AS M2 
	INNER JOIN blog AS B2 ON B2.CustomerID = M2.CustomerID AND B2.PostDate = M2.MaxDate) ON M2.CustomerID = C.CustomerID 
WHERE (E.Username = @username) AND (C.ActiveCust = 1) 
ORDER BY C.LastLogin DESC

Open in new window

0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

jtuttle99Author Commented:
pssandhu:
Yes, some has duplicate results and some doesn't.
The duplucate results are exactly same results.
so looks like this

john
john
mark
brad
james
james
bob



mwvisa1:
I tried your code, but I got exactly same results (some duplicate displays)

Thank you
0
jtuttle99Author Commented:
oh, I'm using SQL server 2008 express edition.
0
Kevin CrossChief Technology OfficerCommented:
Can you display the full row of two of the duplicate records, please.  That will help construct query to filter them out.  One thought with SQL Server 2008 is to use ROW_NUMBER() function with an OVER analytical statement; however, would have to know the exact columns that make the row unique/duplicate.
0
jtuttle99Author Commented:
Here is the screen shot. It's pretty long list, so I cut it half
screenshot.14.jpg
0
Kevin CrossChief Technology OfficerCommented:
CustomerID 3873 is the duplicate record in your picture from what I can see and it has two different LastLogin values, so you can see if this gets what you want.  If it does, then you may need to group by all the columns except for the LastLogin on which you will need to use an aggregate function like MAX or MIN.

The other records all had different data in random columns despite all having first name Shiny.  If that is what you are referring to as duplicates, then you will have to decide which is the valid one to keep as DISTINCT won't work there.
SELECT C.CustomerID, C.FirstName, C.LastName,  C.Email, C.Password
, C.Address, C.City, C.State, C.Country, C.Post
, C.Phone, C.BestTimes, C.CreateDate, /*C.LastLogin,*/ C.ContactPref
, C.EstGiven, C.JobFini, C.Deposit, C.ActiveCust, C.CustNotes
, B1.ImageID AS LatestImageID, B2.Comment AS LatestComment, B2.Poster, B2.PostDate AS Postdate 
FROM Customers AS C 
LEFT OUTER JOIN Employees AS E ON C.empID = E.empID 
LEFT OUTER JOIN ((SELECT CustomerID, MAX(PostDate) AS MaxDate FROM blog WHERE (ImageID <> '') GROUP BY CustomerID) AS M1
        INNER JOIN blog AS B1 ON B1.CustomerID = M1.CustomerID AND B1.PostDate = M1.MaxDate) ON M1.CustomerID = C.CustomerID 
LEFT OUTER JOIN ((SELECT CustomerID, MAX(PostDate) AS MaxDate FROM blog AS blog_1 WHERE (Comment <> '') GROUP BY CustomerID) AS M2 
        INNER JOIN blog AS B2 ON B2.CustomerID = M2.CustomerID AND B2.PostDate = M2.MaxDate) ON M2.CustomerID = C.CustomerID 
WHERE (E.Username = @username) AND (C.ActiveCust = 1) 
ORDER BY C.LastLogin DESC

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
jtuttle99Author Commented:
Yes, it did work if I get rid of LastLogin.
Somehow, I got duplicate data in Customer table.
I have no idea when this happened, but I erased the table and imported again.

Thank you very much for your 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
Query Syntax

From novice to tech pro — start learning today.