Skip to content
Advertisement

Return matched-pair and the non-matched pair for each ID only once

Any help would be appreciated.

I have two sample tables here.

Table A:

Table B

Here is the simple join output and I will explain my question in the comments:

*Yes — I want this row

*No — I don’t want this row

My desired result:

SQL server 2017.

Thank you in advance.


My approach (Inspired by the post from @The Impaler)

Advertisement

Answer

Often when you think about the logic you want in a different way, the answer (or at least AN answer) becomes obvious.

I am thinking of your logic this way:

JOIN Table A to Table B such that A.ID=B.ID (always) AND EITHER A.Name=B.Name OR A.Name doesn’t have a Match in B, and B.Name doesn’t have a match in A.

This logic is pretty easy to express in SQL

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement