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
 Old Forums
 CLOSED - General SQL Server
 Using DISTINCT with multiple columns?

Author  Topic 

brendita
Starting Member

38 Posts

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

SELECT DISTINCT ci.Complex_Name
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!

jhermiz

3564 Posts

Posted - 2005-12-09 : 11:46:07
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 -- [url]http://www.web-impulse.com[/url]
Go to Top of Page

brendita
Starting Member

38 Posts

Posted - 2005-12-09 : 13:00:44
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

jhermiz

3564 Posts

Posted - 2005-12-09 : 13:40:32
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 -- [url]http://www.web-impulse.com[/url]
Go to Top of Page

uuthanh
Starting Member

3 Posts

Posted - 2007-04-18 : 17:18:29
Brendita,

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)
CustomerID
CustomerName
CustomerAddress
CustomerCity
CustomerState
CustomerZip
CustomerPhone
CustomerRate

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

eltombro
Starting Member

1 Post

Posted - 2007-04-30 : 11:08:55
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!
Go to Top of Page
   

- Advertisement -