Help in Compare two tables for any difference

Mike 341 Reputation points
2023-04-12T18:22:39.0333333+00:00

Hello- I want to compare two table's data and if found any difference in any column then these only want to show in the result, as showed in the Expected Result. They key of mapping is Tname, Code and PerID. All columns except key columns (Tname, Code, PerID) are showing in the result to see the difference. If data is matching then should should blank. Create table TableA (TName varchar(20), Code varchar(20), PerID varchar(20), City Varchar(20), Post Varchar(10), Add Varchar(20)) Insert into TableA values ('AAAA','AA','001','HH','M01','121 Cres') Insert into TableA values ('BBBB','AD','002','DD','N01','631 Dr') Create table TableB (TName varchar(20), Code varchar(20), PerID varchar(20), City varchar(20), Post varchar(10), Add varchar(20)) Insert into TableB values ('AAAA','AA','001',GG','M01','121 Cres') Insert into TableB values ('BBBB','AD','002','KK','K01','631 Dr') Expected Result: Tname Code PerID City_A CityB PostA Post_B AddA AddB AAAA AA 001 HH GG BBBB AD 002 DD KK N01 K01

SQL Server | Other
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-04-13T02:33:50.7466667+00:00

    Hi @Mike

    select A.TName,A.Code,A.PerID,
           case when A.City = B.City then '' else A.City end as City_A,
    	   case when A.City = B.City then '' else B.City end as City_B,
    	   case when A.Post = B.Post then '' else A.Post end as Post_A,
    	   case when A.Post = B.Post then '' else B.Post end as Post_B,
    	   case when A.[Add] = B.[Add] then '' else A.[Add] end as Add_A,
           case when A.[Add] = B.[Add] then '' else B.[Add] end as Add_B
    from TableA as A inner join TableB as B on A.TName = B.TName
    

    Output: User's image

    Best regards, Percy Tang

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2023-04-12T21:33:57.79+00:00

    Here is a quick generic query to compare to tables with the same set of columns (and same column order!). The query returns all rows there there are differences.

    SELECT *
    FROM  A
    FULL JOIN B ON A.keycol1 = B.keycol1 AND A.keycol2 = B .keycol2 ...
    WHERE NOT EXISTS (SELECT A.* INTERSECT SELECT B.*)
    
    

    It does not give you the format you want, but I started there since it's short can concise. Although, if you want to exempt column from the comparison, you will need replace A.* and B.* with column lists. For your desired result, the SELECT list will have to be more long-winding like:

    SELECT isnull(A.keycol1, B.keycol1) AS keycol1, isnull(A.keycol2, B.keycol2), ...
           CASE WHEN NOT EXISTS (SELECT A.plaincol1 INTERSECT SELECT B.plaincol1) THEN A.plaincol1 END AS "A.plaincol1",
           CASE WHEN NOT EXISTS (SELECT A.plaincol1 INTERSECT SELECT B.plaincol1) THEN B.plaincol1 END AS "B.plaincol1",
           CASE WHEN NOT EXISTS (SELECT A.plaincol2 INTERSECT SELECT B.plaincol2) THEN A.plaincol2 END AS "A.plaincol2",
           CASE WHEN NOT EXISTS (SELECT A.plaincol2 INTERSECT SELECT B.plaincol2) THEN B.plaincol2 END AS "B.plaincol2",
           ...
    FROM A
    FULL JOIN B ON A.keycol1 = B.keycol1 AND A.keycol2 = B .keycol2 ...
    WHERE NOT EXISTS (SELECT A.* INTERSECT SELECT B.*)
    
    

    The point with INTERSECT here is that it also will handle the case when the column in A is NULL and the column in B is not. For a longer discussion about this, see this blogpost of mine: https://sqlservergeeks.com/a-tip-about-intersect/


  2. Anonymous
    2023-04-13T02:06:05.5366667+00:00

    Hi @Mike
    If I understand correctly, you can try this query.

    select A.TName,A.Code,A.PerID,
           case when A.City = B.City then null else A.City end as City_A,
    	   case when A.City = B.City then null else B.City end as City_B,
    	   case when A.Post = B.Post then null else A.Post end as Post_A,
    	   case when A.Post = B.Post then null else B.Post end as Post_B,
    	   case when A.[Add] = B.[Add] then null else A.[Add] end as Add_A,
           case when A.[Add] = B.[Add] then null else B.[Add] end as Add_B
    from TableA as A inner join TableB as B on A.TName = B.TName
    

    Output: User's image

    Best regards, Percy Tang

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


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.