2

Update:

Unfortunately, the answer provided here did not give me what I wanted - My mistake for accepting too quickly without really doing a deep enough check.

I have reported the question here.


Suppose I have the following two tables in my SQL Server (2012) DB:

Tbl1:

ID:     Col1:     Col2:     Col3:
1       Val1      Val2      Val3
2       <NULL>    Val2      <NULL>
3       Val1      <NULL>    Val3
4       Val1      <NULL>    Val3

Tbl2:

ID:     Col1:     Col2:     Col3:
1       Val1      Val2      Val3
2       <NULL>    Val2      <NULL>
3       <NULL>    <NULL>    Val3
5       <NULL>    <NULL>    Val3

And, at this point, all I want to see is:

  1. Any rows that are in one table but not the other (based on ID pk)
  2. If the ID is in both tables, then are the same columns populated (not caring specifically about the values yet).

I'm just trying to come up with a SQL to just let me know which IDs have discrepancies.

My ideal output would look as follows:

Tbl1_ID:       Tbl2_Id:       Discrepancy:
1              1              0
2              2              0
3              3              1
4              <NULL>         1
<NULL>         5              1  

My testing SQL so far is this:

DECLARE 
@Tbl1 TABLE (ID INT, Col1 VARCHAR(10), Col2 VARCHAR(10), Col3 VARCHAR(10))
;
DECLARE
@Tbl2 TABLE (ID INT, Col1 VARCHAR(10), Col2 VARCHAR(10), Col3 VARCHAR(10))
;

INSERT INTO @Tbl1 (ID, Col1, Col2, Col3)
VALUES
    (1, 'Val1', 'Val2', 'Val3')
    ,(2, NULL   , 'Val2', NULL)
    ,(3, 'Val1', NULL, 'Val3')
    ,(4, 'Val1', NULL, 'Val3')
;

INSERT INTO @Tbl2 (ID, Col1, Col2, Col3)
VALUES
    (1, 'Val1', 'Val2', 'Val3')
    ,(2, NULL   , 'Val2', NULL)
    ,(3, NULL, NULL, 'Val3')
    ,(5, NULL, NULL, 'Val3')
;


SELECT
     [@Tbl1].ID AS Tbl1_ID
    ,[@Tbl2].ID AS Tbl2_ID
    , -- Some kind of comparison to let me know if all columns are populated the same
     AS Discrepancy
FROM
    @Tbl1
    FULL JOIN @Tbl2
    ON [@Tbl1].ID = [@Tbl2].ID

Is there some good way to accomplish this?

Thanks!!

John Bustos
  • 19,036
  • 17
  • 89
  • 151

1 Answers1

2

For the discrepancy column, just use a case statement.

case when [@Tbl1].ID = [@Tbl2].ID then 0 else 1 end

Or for all columns...

case case when [@Tbl1].ID = [@Tbl2].ID 
               and isnull([@Tbl1].Col1,'random') = isnull([@Tbl2].Col1,'random')
               and ...
               then 0 else 1 end
S3S
  • 24,809
  • 5
  • 26
  • 45
  • no, `NULL` is the lack of a value, so it would be a discrepancy. Would you want nulls ignored? – S3S Aug 15 '18 at 19:42
  • 1
    Yes - I said it in the question - If both tables have `NULL` in the same column then it's ok. – John Bustos Aug 15 '18 at 19:43
  • That would do it! - DUH! – John Bustos Aug 15 '18 at 19:45
  • No worries, i should have read better. You asked a clear question +1 – S3S Aug 15 '18 at 19:50
  • THANK YOU for the fast response - Just annoyed I didn't think of that! :) – John Bustos Aug 15 '18 at 19:59
  • Unfortunately, I was too quick to assume the answer was correct - Unfortunately, this still checks the values in `Tbl1.ColX` with those in `Tbl2.ColX` whereas, at this point, I just want to check is both columns are populated **without a comparison on values directly** - I, unfortunately, had to re-ask the question here: https://stackoverflow.com/q/51972449/1693085 Do you have any ideas? – John Bustos Aug 22 '18 at 17:46
  • If you asked a new question after finding new details why remove this one as accepted which doesn’t affect your new question. Quite odd. Anywho I’ll look at the new one – S3S Aug 22 '18 at 17:51
  • I don't mind leaving this as the accepted answer - I just didn't want someone who was looking at this and had the **exact same issue** to assume this was correct since this does compare values whereas the question states I did not want to compare. – John Bustos Aug 22 '18 at 17:54