Query to compare two views and find non-matching record ??

  • rk1980factor (5/12/2016)


    Chris, Looks like you solution might work. Only point i forgot to mention was that both the views(tables) are in different database

    On the same server? Then use three-part naming.

    On different servers? You might have issues with performance.

    “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

  • it is on same server but the 3 columns we are comparing have different names in both tables, not sure if that can be the issue

  • Chris i believe your solution will work with except between two queries. One major problem with 3rd column comparision. In first table value of 3rd column is 0 but in second table the value of 3rd column is NULL. technically both are different but i want them to consider as same. Because of that issue it is giving me all the rows as result.

    To give you one example: Org_Num solumn in Employee table has value 0 in many rows, Org_Num column in Class table as value "NULL" in many rows. But when i run this query

    with EXCEPT

    It displays all the rows are different. I want both 0 and NULL should consider same, should match and don't want that to consider different value.

  • In that case, just wrap your columns that potentially have NULLs in them with ISNULL(column, 0). In the case you gave, you could do this:

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee

    EXCEPT

    SELECT [Emp_no.], Org_Code, ISNULL(Org_Num,0) FROM #Class

    -- get rows which are unique to Class:

    SELECT [Emp_no.], Org_Code, ISNULL(Org_Num,0) FROM #Class

    EXCEPT

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee

  • I might be missing something, but couldn't this be done in a single query like so?

    SELECT

    *

    FROM

    #Employee Employee

    FULL OUTER JOIN

    #Class Class

    ON

    IsNull(Employee.[Emp_no.], '') = IsNull(Class.[Emp_no.], '')

    AND IsNull(Employee.Org_Code, '') = IsNull(Class.Org_Code, '')

    AND IsNull(Employee.Org_Num, '') = IsNull(Class.Org_Num, '')

    WHERE

    (

    Employee.[Emp_no.] IS NULL

    AND Employee.Org_Code IS NULL

    AND Employee.Org_Num IS NULL

    )

    OR

    (

    CLASS.[Emp_no.] IS NULL

    AND CLASS.Org_Code IS NULL

    AND CLASS.Org_Num IS NULL

    )

  • wish this can be done easily with single query :

    actually this query provided by previous user does work:

    SELECT Emp_no, Org_Code, Org_Num FROM [dbo].[Employee]

    EXCEPT

    SELECT Emp_no, Org_Code, Org_Num FROM [dbo].[Class]

    -- get rows which are unique to Class:

    SELECT Emp_no, Org_Code, Org_Num FROM [dbo].[Class]

    EXCEPT

    SELECT Emp_no, Org_Code, Org_Num FROM [dbo].[Employee]

    but it gives me result in two separate windows.

    I want the result should be in same window

  • below query gives me wrong result , it seem to list all the values

    SELECT

    *

    FROM

    #Employee Employee

    FULL OUTER JOIN

    #Class Class

    ON

    IsNull(Employee.[Emp_no.], '') = IsNull(Class.[Emp_no.], '')

    AND IsNull(Employee.Org_Code, '') = IsNull(Class.Org_Code, '')

    AND IsNull(Employee.Org_Num, '') = IsNull(Class.Org_Num, '')

    WHERE

    (

    Employee.[Emp_no.] IS NULL

    AND Employee.Org_Code IS NULL

    AND Employee.Org_Num IS NULL

    )

    OR

    (

    CLASS.[Emp_no.] IS NULL

    AND CLASS.Org_Code IS NULL

    AND CLASS.Org_Num IS NULL

    )

  • Looks like this query displays the correct result but it display on separate window. i want one query which will show me one result:

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee

    EXCEPT

    SELECT [Emp_no.], Org_Code, ISNULL(Org_Num,0) FROM #Class

    -- get rows which are unique to Class:

    SELECT [Emp_no.], Org_Code, ISNULL(Org_Num,0) FROM #Class

    EXCEPT

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee

  • rk1980factor (5/13/2016)


    Looks like this query displays the correct result but it display on separate window. i want one query which will show me one result:

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee

    EXCEPT

    SELECT [Emp_no.], Org_Code, ISNULL(Org_Num,0) FROM #Class

    -- get rows which are unique to Class:

    SELECT [Emp_no.], Org_Code, ISNULL(Org_Num,0) FROM #Class

    EXCEPT

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee

    Set each query up as a CTE then concatenate them using UNION ALL.

    “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

  • rk1980factor (5/13/2016)


    below query gives me wrong result , it seem to list all the values

    SELECT

    *

    FROM

    #Employee Employee

    FULL OUTER JOIN

    #Class Class

    ON

    IsNull(Employee.[Emp_no.], '') = IsNull(Class.[Emp_no.], '')

    AND IsNull(Employee.Org_Code, '') = IsNull(Class.Org_Code, '')

    AND IsNull(Employee.Org_Num, '') = IsNull(Class.Org_Num, '')

    WHERE

    (

    Employee.[Emp_no.] IS NULL

    AND Employee.Org_Code IS NULL

    AND Employee.Org_Num IS NULL

    )

    OR

    (

    CLASS.[Emp_no.] IS NULL

    AND CLASS.Org_Code IS NULL

    AND CLASS.Org_Num IS NULL

    )

    Do you mean all the values as in all the columns instead of just the three key columns?

    You can change the * to whatever columns you want to see.

  • don't know anything about CTE's

    i tried this query after reading some articles but it does not work

    ;WITH CTE1 AS SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee

    EXCEPT

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Class

    union all

    ;WITH CTE2 AS

    -- get rows which are unique to Class:

    (SELECT [Emp_no.], Org_Code, Org_Num FROM #Class

    EXCEPT

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee)

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee

    EXCEPT

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Class

    UNION ALL

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Class

    EXCEPT

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee

  • thought it should be fairly simple to display one result with two queries or combining two queries

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee

    EXCEPT

    SELECT [Emp_no.], Org_Code, ISNULL(Org_Num,0) FROM #Class

    -- get rows which are unique to Class:

    SELECT [Emp_no.], Org_Code, ISNULL(Org_Num,0) FROM #Class

    EXCEPT

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee

  • Have you tried this?

    ;WITH

    CTE1 AS (

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee

    EXCEPT

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Class

    ), CTE2 AS (

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Class

    EXCEPT

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee

    )

    SELECT * FROM CTE1

    UNION ALL

    SELECT * FROM CTE2

    “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

  • ChrisM@Work (5/13/2016)


    Have you tried this?

    ;WITH

    CTE1 AS (

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee

    EXCEPT

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Class

    ), CTE2 AS (

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Class

    EXCEPT

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee

    )

    SELECT * FROM CTE1

    UNION ALL

    SELECT * FROM CTE2

    No need to set up two CTEs.

    (

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee

    EXCEPT

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Class

    )

    UNION ALL

    (

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Class

    EXCEPT

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee

    )

    You can simply use parentheses to ensure that the EXCEPT clauses are evaluated before the UNION clause.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • so both of this query works:

    ;WITH

    CTE1 AS (

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee

    EXCEPT

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Class

    ), CTE2 AS (

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Class

    EXCEPT

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee

    )

    SELECT * FROM CTE1

    UNION ALL

    SELECT * FROM CTE2

    No need to set up two CTEs.

    (

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee

    EXCEPT

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Class

    )

    UNION ALL

    (

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Class

    EXCEPT

    SELECT [Emp_no.], Org_Code, Org_Num FROM #Employee

    )

    final question on these is, i still want to compare all 3 columns from both tables but just want to display one column only (Emp_no.) without duplicates.

Viewing 15 posts - 16 through 30 (of 32 total)

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