If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > SELECT DISTINCT in multiple colums

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-16-04, 20:10
alski alski is offline
Registered User
 
Join Date: Dec 2002
Posts: 24
SELECT DISTINCT in multiple colums

Hi All,

In Table1 I have the following columns:

primary
RecordID.....Surname1.....Surname2....Surname3
...101...........Adams.........Ackman.......Adams
...102...........Barnes..........Adams........Bake r
...103...........Charles..........Kemp .......Tomas

I would like the following result:
Ackman
Adams
Andrews
Baker
Barnes
Charles
Kemp
Tomas

Note there are no duplicate of 'Adams'.

I can't work out the correct "SELECT DISTINCT" query

Can anyone help??

Cheer,
Alski
Reply With Quote
  #2 (permalink)  
Old 07-16-04, 22:33
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
select Surname1
  from Table1
union
select Surname2
  from Table1
union
select Surname3
  from Table1
duplicates removed automatically

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 07-17-04, 01:33
alski alski is offline
Registered User
 
Join Date: Dec 2002
Posts: 24
Cheers Rudy - that worked so well (you don't know how many hours you've just saved me!).

After reading up on UNION's I see now that they effectively remove duplicates whereas a JOIN wouldn't - does that sound about right?


Alski
Reply With Quote
  #4 (permalink)  
Old 07-17-04, 01:44
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
Quote:
Originally Posted by alski
After reading up on UNION's I see now that they effectively remove duplicates whereas a JOIN wouldn't - does that sound about right?
As a side note, your original set did not contain any duplicates.
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.
Reply With Quote
  #5 (permalink)  
Old 07-17-04, 06:36
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by r123456
As a side note, your original set did not contain any duplicates.
Adams -- 101 Surname3 and 102 Surname2

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On