Solved

SQL DISTINCT shows 2 same results

Posted on 2009-07-07
9
246 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:jtuttle99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24799181
Are they exactly the same or is there one column that is different?

What is your SQL platform and version?
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24799184
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
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24799208
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jtuttle99
ID: 24799506
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
 

Author Comment

by:jtuttle99
ID: 24799562
oh, I'm using SQL server 2008 express edition.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24799589
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
 

Author Comment

by:jtuttle99
ID: 24799639
Here is the screen shot. It's pretty long list, so I cut it half
screenshot.14.jpg
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 24799662
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
 

Author Comment

by:jtuttle99
ID: 24799855
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Pivot table 2 53
How to trim a value in SQL 2 50
How to add an Index to a date/time field? 15 41
Populating a table from inside a trigger 2 14
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question