Solved

Simple distinct join of two tables in SQL

Posted on 2013-01-14
4
786 Views
Last Modified: 2013-01-14
I know this has to be easy but honestly I dont have the time to figure it out.

I have two tables with a single column... LastName
Table A
Smith
Jones

Table B
Jones
Williams

How can I get a single result set... with distinct values... without using a temp table...

ie.
Smith
Jones
Williams

On a single table I would usually do "select distinct(LastName) from TableA"...

Thanks!
0
Comment
Question by:James Talvy
  • 2
4 Comments
 
LVL 26

Expert Comment

by:wilcoxon
Comment Utility
I don't have a good answer in sql for this.  I'd use a temp table, pipe it through Unix commands (I use sqsh for accessing Sybase), or whip up a quick perl script.
0
 

Author Comment

by:James Talvy
Comment Utility
Can't do that since I need to use this as an intermediary step within a stored procedure...
0
 
LVL 11

Accepted Solution

by:
Simone B earned 400 total points
Comment Utility
Will this work?

select distinct LastName from TableA
union
select distinct LastName from TableB

Using DISTINCT eliminates duplicates within each table, while using UNION (instead of UNION ALL) should eliminate the duplicates between the two tables.
0
 

Author Closing Comment

by:James Talvy
Comment Utility
Indeed it does seem to work thanks.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now