Compare data for 4 columns

  • pwalter83

    SSChampion

    Points: 14557

    Hi,

    Can anybody please help with a SQL query to compare 4 column values between 2 tables to check if any data changes have taken place for a unique row ID ?

    Many thanks.

     

     

    • This topic was modified 2 months, 3 weeks ago by  pwalter83.
  • drew.allen

    SSC Guru

    Points: 76721

    You've been around long enough to know that you should provide sample data and expected results.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Jonathan AC Roberts

    SSCoach

    Points: 17275

    Here's a guess at what you want:

    (
    SELECT 'Table1' TableName,ID,Col1,Col2,Col3,Col4
    FROM Table1 t1
    WHERE EXISTS(SELECT * FROM Table2 t2 WHERE t2.ID = t1.ID)
    EXCEPT
    SELECT 'Table1' TableName,ID,Col1,Col2,Col3,Col4
    FROM Table2
    )
    UNION ALL
    (
    SELECT 'Table2' TableName,ID,Col1,Col2,Col3,Col4
    FROM Table2 t2
    WHERE EXISTS(SELECT * FROM Table1 t1 WHERE t1.ID = t2.ID)
    EXCEPT
    SELECT 'Table2' TableName,ID,Col1,Col2,Col3,Col4
    FROM Table1
    )

     

     

     

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply