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
 Using DISTINCT

Author  Topic 

sford
Starting Member

4 Posts

Posted - 2007-04-06 : 17:49:17
Is there some way to use the distinct keyword so that it applies ONLY to a subset of the items in the select list???

For example, suppose I want to
select col_1, col_2, col_3, col_4

but I do not want distinct applied against all 4 items...
Maybe I want all 4 items in the selection list,
but I want distinct to use only col_3 as its filtration criteria...

I know the syntax shown below is not valid, but I am showing it anyway because I am hoping it will illustrate what I am looking for...

Is there a VALID syntax that is something like this???

select col_1, col_2, (distinct col_3), col_4
or
select col_1, col_2, distinct (col_3), col_4



rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2007-04-06 : 18:38:09
If you can provide a sample input, and the desired output from that query,
it will help both us (and You) to understand what you are trying to achieve.

rockmoose
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-04-06 : 18:38:14
To do this, you would use a GROUP BY.

But please show us a data example so that we can help you with the code.

Tara Kizer
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

sford
Starting Member

4 Posts

Posted - 2007-04-06 : 20:03:55

Hypothetical data set
# | Name | Comp | Rela
----------------------
1 | Stev | ABCD | OOOO
2 | Joan | efgh | ZZZZ
3 | Arlo | 1234 | ZZZZ
4 | Arlo | 1234 | XXXX
5 | Mike | ZYXW | ZZZZ

Desired Result
Name | Comp | Rela
------------------
Stev | ABCD | OOOO
Joan | efgh | ZZZZ
Arlo | 1234 | ZZZZ
Mike | ZYXW | ZZZZ

Equally Desirable Result
Name | Comp | Rela
------------------
Stev | ABCD | OOOO
Joan | efgh | ZZZZ
Arlo | 1234 | XXXX
Mike | ZYXW | ZZZZ

If I do this...
Select distinct Name, Comp

I get this...
Name | Comp
-----------
Stev | ABCD
Joan | efgh
Arlo | 1234
Mike | ZYXW

This is limited to just the records I want, but does not include the Rela field

On the other hand, If I do this...
Select distinct Name, Comp, Rela

I get this...
Name | Comp | Rela
------------------
Stev | ABCD | OOOO
Joan | efgh | ZZZZ
Arlo | 1234 | ZZZZ
Arlo | 1234 | XXXX
Mike | ZYXW | ZZZZ

This has the Rela field, but it has the Arlo record duplicated...

I want all three fields, but I want only ONE Arlo record...
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-04-06 : 22:45:47
[code]
select Name, Comp, max(Rela) -- or min()
from table
group by Name, Comp
[/code]


KH

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-04-07 : 03:15:33
Also you need to Learn SQL

http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -