SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Comparing Two Different Tables, Showing Values In Table 1 Not in Table 2


Comparing Two Different Tables, Showing Values In Table 1 Not in Table 2

Author
Message
VegasL
VegasL
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 159
Hello,

I'd like to compare two tables, and show values found in table 1 which are NOT in table 2, then same for table 2, show values in table 2 which are NOT in table 1.

Here's what I came up with, looks ok?

select T1.[Name], T2.[Address]
from [dbo].[My Table 1] as T1
left join [dbo].[My Table 2] as T2 on T1.[Name]=T2.[Name]
where T2.[Address] is not null

Thanks
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16484 Visits: 19557
SELECT
TableSource = CASE WHEN T1.[Name] IS NULL THEN 'Table2' ELSE 'Table1' END,
T1.[Name],
T1.[Address],
T2.[Name],
T2.[Address]
FROM [dbo].[My Table 1] T1
FULL OUTER JOIN [dbo].[My Table 2] T2
ON T1.[Name] = T2.[Name]
WHERE NOT T1.[Name] = T2.[Name]



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search