How to show Missing Data Using LeftOuterJoin or Any Join Methods

Carlton Patterson 41 Reputation points
2020-09-05T14:46:20.727+00:00

Hello Forum

I have an original table and data as follows:
CREATE TABLE #tmpTable (
id int,
FirstName varchar(50),
LastName varchar(50),
Email varchar(50),
Department varchar(50),
Salary int)

INSERT #tmpTable VALUES  
(6,'RameshTheThird','Babu','******@pramati.co','Politics',15000),  
(2,'Daniel','meng','******@stanford.edu','Delivery',20000),  
(8,'Sampath','Kumar','******@pramati.co','Advertising',16000),  
(7,'Bipul','Kumar','******@pramati.co','Research',14000),  
(3,'Muriel',NULL,'******@waterloo.edu','Marketing',13000),  
(1,'Basher','armbrust','******@gmail.com','HR',10000),  
(4,'Rachel','wendell','******@imaginea.com','Engineering',70000),  
(5,'Zach','galifianakis','******@pramati.co','Analyst',87777),  
(9,'Anil','Reddy','******@pramati.co','Business',78000),  
(10,'Mageswaran','Dhandapani','******@pramati.co','Law',88000)  

I then an updated table as follows:

CREATE TABLE #tmpTable (  
    id int,  
    FirstName varchar(50),  
    LastName varchar(50),  
    Email varchar(50),  
    Department varchar(50),  
    Salary int)  
  
INSERT #tmpTable VALUES  
(13,'Carlton','Patterson','******@keyloop.co.uk','Data Engineering',80000),  
(10,'Mageswaran','Dhandapani','******@pramati.co','Law',88000),  
(3,'Muriel','Somebody','******@waterloo.edu','BrickLayer',66000),  
(6,'RameshTheThird','Babu','******@pramati.co','Politics',15000),  
(4,'Rachel','wendell','******@imaginea.com','Engineering',70000),  
(2,'Daniel','Peterson','******@stanford.edu','Scientist',20000),  
(5,'Zach','galifianakis','******@pramati.co','Analyst',87777),  
(8,'Sampath','Kumar','******@pramati.co','Advertising',16000),  
(7,'Bipul','Kumar','******@pramati.co','Research',14000),  
(9,'Anil','Reddy','******@pramati.co','Business',78000),  
(1,'Basher','armbrust','******@gmail.com','HR',666)  

As you can see from the updated table there has been some changes to Department, Salaries and LastName values

I have compiled the following code with varioius leftouterjoin to capture the updates in the second updated table

SELECT  
  ORIGINALv1.FirstName  
 ,ORIGINALv1.LastName  
 ,ORIGINALv1.Department  
 ,ORIGINALv1.Salary  
 ,UPDATED.Salary  
 ,UPDATED.Department  
 ,UPDATED.LastName  
FROM dbo.ORIGINALv1  
LEFT OUTER JOIN dbo.UPDATED  
  ON ORIGINALv1.Salary = UPDATED.Salary  
    AND ORIGINALv1.Department = UPDATED.Department  
    AND ORIGINALv1.LastName = UPDATED.LastName  

The table names have changed in my code, but the data is the same.

My code doesn't actually show the changes.

Can someone show me how to modify my query to show the changes?

Developer technologies | Transact-SQL
0 comments No comments
{count} votes

Accepted answer
  1. Guoxiong 8,206 Reputation points
    2020-09-05T17:48:20.077+00:00

    Try this:

    -- Updated rows
    SELECT 
      UPDATED.id
     ,UPDATED.FirstName
     ,UPDATED.LastName
     ,UPDATED.Department
     ,UPDATED.Salary
    FROM dbo.UPDATED
    INNER JOIN dbo.ORIGINALv1 ON UPDATED.id = ORIGINALv1.id AND (
     UPDATED.FirstName <> ORIGINALv1.FirstName OR 
     UPDATED.Department <> ORIGINALv1.Department OR 
     UPDATED.Salary <> ORIGINALv1.Salary
    )
    UNION
    -- New rows
    SELECT 
      UPDATED.id
     ,UPDATED.FirstName
     ,UPDATED.LastName
     ,UPDATED.Department
     ,UPDATED.Salary
    FROM dbo.UPDATED
    WHERE UPDATED.id NOT IN (SELECT id FROM ORIGINALv1)
    ORDER BY id;
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2020-09-05T15:17:49.3+00:00

    Hello.
    If you want to see any changes (if any) you can edit the query this way (assuming, as it appears, the id field is the primary key).

       SELECT
           ORIGINALv1.FirstName
          ,ORIGINALv1.LastName
          ,ORIGINALv1.Department
          ,ORIGINALv1.Salary
          ,UPDATED.Salary
          ,UPDATED.Department
          ,UPDATED.LastName
         FROM dbo.ORIGINALv1
         LEFT OUTER JOIN dbo.UPDATED
           ON UPDATED.id = ORIGINALv1.id
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.