I'm trying to find a syntax that will take data from one table, compare it to another table, and return any anomalies...
By submitting your email address, you agree to receive emails regarding relevant topic offers from TechTarget and its partners. You can withdraw your consent at any time. Contact TechTarget at 275 Grove Street, Newton, MA.
that exist. Is this possible?
Most definitely possible.
select A.pk , A.col1, B.col1 , A.col2, B.col2 , A.col3, B.col3 from table_one as A inner join table_two as B on B.pk = A.pk where A.col1 <> B.col1 or A.col2 <> B.col2 or A.col3 <> B.col3 union all select A.pk , A.col1, null , A.col2, null , A.col3, null from table_one as A left outer join table_two as B on B.pk = A.pk where B.pk is null union all select B.pk , null, B.col1 , null, B.col2 , null, B.col3 from table_two as B left outer join table_one as A on A.pk = B.pk where A.pk is null
The inner join, with its WHERE conditions, returns rows which exist in both tables but which have at least one column different. Then the two outer joins, with their WHERE conditions, return rows which exist in one table but not the other. The results of these three subqueries are combined with UNION ALL to return all anomalies.
Related Q&A from Rudy Limeback
Read SQL expert Rudy Limeback's advice for counting combinations in a table with SQL's GROUP BY clausecontinue reading
Read an example of an SQL case expression from our SQL expert Rudy Limeback.continue reading
Read about the Mimer Validator, a tool used to verify your SQL code, in this tip from SQL expert Rudy Limeback.continue reading
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.