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
 DISTINCT

Author  Topic 

gems
Starting Member

19 Posts

Posted - 2007-03-13 : 10:07:45
Hi All,

I am using SQL Server 2000. I have 3 fields in my table. I want to do distinct on one field but also want to show the remaining two fields in the result. How can I do that?

Thanks

-G

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-13 : 10:13:12
[code]
select field1, min(field2), min(field3)
from mytable
group by field1
[/code]


KH

Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-03-13 : 10:30:27
why not MAX?

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-13 : 10:35:56
OP never specify the requirement and typing min only required one hand.


KH

Go to Top of Page

gems
Starting Member

19 Posts

Posted - 2007-03-13 : 11:10:02
thanks khtan. that works fine. I am pretty new to this. Can you please explain a little as to how this is working?

Thanks again.
Go to Top of Page

JohnH
Starting Member

13 Posts

Posted - 2007-03-13 : 13:47:07
Suppose you have values
'Bob', 1, 2
'Bob', 1, 1

You might want to see MIN():
'Bob', 1, 1

Or you might want to see MAX():
'Bob', 1, 2

Or you might want to see SUM():
'Bob', 2, 3

Or you might want to see AVG():
'Bob', 1, 1.5

Or you might want to see something else entirely. You didn't tell us enough, so khtan gave you something to think about. You need to decide what you want to do with the values in those other two columns.


John Hopkins
Go to Top of Page

gems
Starting Member

19 Posts

Posted - 2007-03-13 : 15:55:38
Thanks John. I understand that. But all my three fields are text fields. So I just wasn't sure how it would work here.
Go to Top of Page
   

- Advertisement -