Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 568531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:11:54+00:00 2026-05-13T13:11:54+00:00

The query below selects the rows from table_1 which do not exist in table_2

  • 0

The query below selects the rows from table_1 which do not exist in table_2 based on values in the 4 columns. The mismatch could be due to one or more columns.

I would like to extend the query to tell me which column(s) had the mismatched values, either by showing the column name or its value. I can do this in a cursor but prefer to do it in a set based operation if possible.

SELECT  i.agent ,
        i.agency ,
        i.customer ,
        i.Company
FROM    table_1 AS i
WHERE   NOT EXISTS ( SELECT p.agent ,
                            p.agency ,
                            p.customer ,
                            p.Company
                     FROM   table_2 AS p
                     WHERE  i.Agent = p.Agent
                            AND i.agency = p.Agency
                            AND i.customer = p.customer
                            AND i.Company = p.Company )

Update:
I guess this needs more refinement. Let’s add that 3 out of 4 columns need to match.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:11:55+00:00Added an answer on May 13, 2026 at 1:11 pm

    You can simplify this problem drastically if you require that certain columns do match, or at least start with some expectations of which columns should match. In other words, instead of looking at this as a non-matching problem, redefine it as a partial matching problem.

    Let’s say you expect agent and agency to match, but customer and company might not. This isn’t too difficult:

    SELECT
        i.agent, i.agency, i.customer, i.company, p.customer, p.company,
        CASE
            WHEN i.customer = p.Customer THEN 'Y'
            ELSE 'N'
        END AS matchescustomer,
        CASE
            WHEN i.company = p.Company THEN 'Y'
            ELSE 'N'
        END AS matchescompany
    FROM table1 i
    INNER JOIN table2 p
        ON p.agent = i.agent
        AND p.agency = i.agency
    

    If you want to check for other partial matches, just reorder the columns. Instead of joining on agent and agency, join on agent and customer, or whatever.

    If you only expect a few different kinds of partial matches, you can write a few different queries similar to the one above and put them together with a UNION (or UNION ALL if you don’t mind duplicates). In other words:

    SELECT (columns)
    FROM table1 i INNER JOIN table2 p
        ON p.agent = i.agent AND p.agency = i.agency
    UNION
    SELECT (columns)
    FROM table1 i INNER JOIN table2 p
        ON p.agent = i.agent AND p.customer = i.customer
    

    Now if you’re looking to get every conceivable mismatch, then this is quickly going to get out of control, so you might want to adapt a more heuristic method, and search for partial matches that match at least a certain number of columns (say 3). Then you can restrict the obnoxiousness to at most the number of columns you have to compare:

    ;WITH PartialMatches_CTE AS
    (
        SELECT i.agent AS iagent, p.agent AS pagent, ... (etc.)
        FROM table1 i INNER JOIN table2 p ON p.agent = i.agent
        UNION ALL
        SELECT (...) FROM table1 INNER JOIN table2 ON p.agency = i.agency
        UNION ALL
        SELECT (...) FROM table1 INNER JOIN table2 ON p.company = i.company
        ... and so on
    ),
    ResolvedMatches_CTE AS
    (
        SELECT DISTINCT
            iagent, pagent, iagency, pagency, ...,
            CASE WHEN pagent = iagent THEN 'Y' ELSE 'N' END AS agentmatch,
            CASE WHEN pagency = iagency THEN 'Y' ELSE 'N' END AS agencymatch,
            ...,
            (CASE WHEN pagent = iagent THEN 1 ELSE 0 END +
             CASE WHEN pagency = iagency THEN 1 ELSE 0 END +
             ...) AS MatchCount
        FROM PartialMatches_CTE
    )
    SELECT *
    FROM ResolvedMatches_CTE
    WHERE MatchCount >= 3
    

    Now, having said all this, there’s one thing I’m wondering…

    These two data tables aren’t, perchance, sequentially related, are they? As in, the 3rd row in table1 always maps to the 3rd row in table 2, but may not match all columns? It’s a shot in the dark, but if that is indeed the case, then we could make this way simpler. Otherwise, the last query here should probably do what you want without becoming too much of an unmaintainable mess.

    Note that performance is likely going to stink for all of these queries. Hopefully your data sets aren’t too large. AFAIK there is no easy way to really optimize this kind of thing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 373k
  • Answers 373k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer See the movie player sample code from the apple docs. May 14, 2026 at 7:34 pm
  • Editorial Team
    Editorial Team added an answer def get(id, user_id=None): query = """SELECT * FROM USERS WHERE… May 14, 2026 at 7:34 pm
  • Editorial Team
    Editorial Team added an answer You convert the read string to an integer with atoi:… May 14, 2026 at 7:34 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.