Solved

Simple distinct join of two tables in SQL

Posted on 2013-01-14
4
796 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
ID: 38775932
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
 
LVL 1

Author Comment

by:James Talvy
ID: 38775942
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
ID: 38776035
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
 
LVL 1

Author Closing Comment

by:James Talvy
ID: 38776144
Indeed it does seem to work thanks.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

910 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

21 Experts available now in Live!

Get 1:1 Help Now