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
 General SQL Server Forums
 New to SQL Server Programming
 Select Distinct

Author  Topic 

BendJoe
Posting Yak Master

128 Posts

Posted - 2007-10-16 : 11:38:30
I have a select query
Select distinct a,b,c,d from xyz
I would like to know what the syntax is if I want only a,b,c to be distinct and not d.
I tried something like
Select (distinct a,b,c),d
but getting error what is the correct query to do this.
Please help.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-10-16 : 11:41:03
SELECT A, B, C, MIN(d)
FROM Table1
GROUP BY A, B, C



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

BendJoe
Posting Yak Master

128 Posts

Posted - 2007-10-16 : 11:47:31
You are telling me to use MIN function and dont use DISTINCT.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-10-16 : 11:57:26
If you use DISTINCT then you will see EVERY value of [d]

So if you want the Distinct values of A, B and C then you need to decide on a single value for [d]. You can have Min, Max or Average easily. Or you could have a random value of [d], or the first one ordered by some column - such as the date the record was created.

With some more effort you can have a list of values (i.e. a comma separate list all-in-the-one-result-column)

Kristen
Go to Top of Page

BendJoe
Posting Yak Master

128 Posts

Posted - 2007-10-16 : 12:00:44
Yes the solution looked a bit strange but it worked.
Thanks a lot.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-10-16 : 12:59:34
well if there are 3 possible values for "D"

Which one do you want?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

BendJoe
Posting Yak Master

128 Posts

Posted - 2007-10-16 : 13:24:31
Yes the above solution will only return a single value for D.
Here is my SQL Select.
SELECT distinct aspnet_Membership.LoweredEmail, aspnet_Membership.IsApproved, aspnet_Users.UserName, BuildingAddress.BuildingAddress,
UserContact.FirstName, UserContact.LastName, UserContact.Company
FROM UserContact INNER JOIN
BuildingAddress ON UserContact.UserID = BuildingAddress.UserID INNER JOIN
aspnet_Membership INNER JOIN
aspnet_Users ON aspnet_Membership.UserId = aspnet_Users.UserId ON UserContact.UserID = aspnet_Users.UserId
WHERE (aspnet_Membership.IsApproved = 1)
AND
((UserName LIKE @UserName) OR (@UserName IS NULL))
AND
((Company LIKE @Company)OR (@Company IS NULL))
AND
((BuildingAddress LIKE @Building) OR (@Building IS NULL))

Here I have four tables aspnet_Membership,aspnet_Users,UserContact,BuildingAddress all these tables can be connected by a join on UserID. The table named BuildingAddress can have upto three records for a unique UserID. So my query is returning 3 records for each userID provided the BuildingAddress are all different for the same UserID. I need the Query not to give me the record if the BuildingAddress.BuildingAddress is empty. Or I need only distinct and not null BuildingAddress. How can this be done?
Go to Top of Page
   

- Advertisement -