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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Compiling a DISTINCT list of values from multiple fields.

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-09-19 : 09:34:28
Ken writes "Can you create a SQL statement to create a DISTINCT list of values from multiple fields? For instance, imagine 3 char fields:

FIELD1 char(5)
FIELD2 char(5)
FIELD3 char(5)

All three fields have five character values and the goal is to create a SQL statement that will give me a distinct list of the values contained in one or more of the three fields.

I hate to admit it, but SQL 6.5 if possible.

All help appreciated!

Ken"

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-09-19 : 09:48:25

select
blah
from
(select field1 as blah from ken
union
select field2 as blah from ken
union
select field3 as blah from ken) as blahs

 


Jay White
{0}
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2002-09-20 : 05:12:09
1 minor addition.....to satisfy the full requirements of the 1st line....

select
DISTINCT(blah)
from
(select field1 as blah from ken
union
select field2 as blah from ken
union
select field3 as blah from ken) as blahs


Go to Top of Page

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2002-09-20 : 07:13:26
The UNION means the values will be distinct already.


Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2002-09-20 : 11:48:04
I stand corrected......it's been too long since Set Theory 1.1 was completed.

Go to Top of Page
   

- Advertisement -