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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 Old Forums
 CLOSED - General SQL Server
 Using DISTINCT with multiple columns?
 Forum Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

38 Posts

Posted - 12/09/2005 :  11:39:19  Show Profile
This query returns part of what I need:

FROM CaseInformation c
INNER JOIN CommunityInformation ci
ON c.complex_ID = ci.Complex_ID
WHERE c.Date_entered BETWEEN '11/01/2005' AND '12/01/2005'

But I also need to return the address, city, etc for each of those Communities. But when I add those to the query, it returns multiple rows. How can I return more information without getting multiple rows for each distinct Community?

Thanks for your time!

Flowing Fount of Yak Knowledge

3564 Posts

Posted - 12/09/2005 :  11:46:07  Show Profile  Visit jhermiz's Homepage
You can't...the query is doing exactly what you are telling it to do, distinct works on a row not a column basis.
Someone asked the same question yesterday. The reason it returns multiple rows is because the COMPLETE row is NOT distinct, adding those other fields makes it UNIQUE.

What you want to read over is GROUP BY vs. DISTINCT as well as aggregate functions.

Keeping the web experience alive --
Go to Top of Page

Starting Member

38 Posts

Posted - 12/09/2005 :  13:00:44  Show Profile
I've been looking for some articles GROUP BY vs. DISTINCT but can't find any that solve my problem. Can you lead me to an article?
Go to Top of Page

Flowing Fount of Yak Knowledge

3564 Posts

Posted - 12/09/2005 :  13:40:32  Show Profile  Visit jhermiz's Homepage
But your question all and all doesn't make sense, you want a unique name from case information with multiple rows...

You cant do that because you have a one to many relationship, your problem is not a data problem, it is a problem with presentation. Dont worry about getting multiple rows of the same data as a data issue, worry about it as a presentation issue.

On a report you would create a group by for the case information and then in the details section you would place all your "many side" data. That way you ONLY see ONE Complex_name and all related data.

Sound right ?

Keeping the web experience alive --
Go to Top of Page

Starting Member

3 Posts

Posted - 04/18/2007 :  17:18:29  Show Profile

Did you figure out how to do it? I got similar problem.
I googled all day, but found nothing beside your post.

I tried to extract out: unique customer records from a trouble ticket table. Simple like this:

TicketID (primary)

If I use DISTINCT then, I can only get back 1 column (CustomerID)
If I use GROUP BY, then I have to group by all the fields above in order to select all. I wish I can GROUP BY only CustomerID, but it let me select all above fields.

How about you, jhermiz? Can you help?

Thanks so much for any clue.
Go to Top of Page

Starting Member

United Kingdom
1 Posts

Posted - 04/30/2007 :  11:08:55  Show Profile  Visit eltombro's Homepage  Click to see eltombro's MSN Messenger address
Finally I've found someone with the same problem!

I'm trying the following:

SELECT DISTINCT value1, value2 FROM table WHERE id = ...

Basically I'm trying to retrieve values where 2 columns are distinct. Unfortunately I can't seem to be able to get the second value, value2, and assign it to an ASP variable from my recordset...

Correction: I debugged my query by finally finding out how to run it in Access 2007. Nothing wrong with the query erm in fact it was my subsequent query which was flawed... I'd got the wrong column name for an id column.

To conclude, selecting distinct values will return them all (distinctly) and are assignable as variables from a recordset. Sorry!

Edited by - eltombro on 04/30/2007 12:08:57
Go to Top of Page
  Previous Topic Topic Next Topic  
 Forum Locked
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000