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
 select distinct but return all records

Author  Topic 

nindoo
Starting Member

4 Posts

Posted - 2004-08-11 : 15:13:15
Hello guys,

I have a table where I would like to SELECT DISTINCT two attributes but display all the attributes of the table.

For example

Lets say I have attributes h1, h2, h3, h4

I want to select distinct h1, h2 for I want to have h1, h2, h3, h4 returned to me.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-08-11 : 15:16:09
SELECT h1, h2, MAX(h3), MAX(h4)
FROM Table1
GROUP BY h1, h2

Tara
Go to Top of Page

nindoo
Starting Member

4 Posts

Posted - 2004-08-11 : 15:48:53
Thank you

Tara it worked like a charm.

:)
Go to Top of Page

Hommer
Aged Yak Warrior

808 Posts

Posted - 2004-08-12 : 10:10:51
Be careful though. The result may be misleading or mismatching.
nindoo:
Why will you do that? It is like you were asking for any h3/h4 value given a h1/h2 pair.
Tara's solution gave you max on h3, h4, but why not min?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-08-12 : 13:15:43
If you are looking for ANY value, then MAX or MIN would work. I am usually after the MAX when I write my GROUP BYs, so that's why I provided MAX in the solution.

Tara
Go to Top of Page
   

- Advertisement -