Skip to content
Advertisement

How to exclude data if any record is not matching in any of the column in the another Table

I have Table A and Table B

I’m looking to join both the tables with all columns in each table. If any record is not matching then we need to remove all the SK.

For 17734 value all the columns from the both tables are matching then I need to get the values for 17734 .

For 16601 value only 1 value is not matching so I don’t want to bring all the values for 16601.

After joining, I get:

Output :

Advertisement

Answer

You must do the whole join to discover if some of the results should be removed. To reuse the query results, use a CTE and refer to it twice – once for the results and again to filter out the unwanted rows:


BTW, you have a bug in your query: You’re joining [dbo].[TableA] with itself, but it should be joined to [dbo].[TableB]

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