## Using FULL JOINs to Compare Datasets

 Author Message 53280comma1 Old Hand Group: General Forum Members Points: 327 Visits: 231 The other option of course is to build the tolerance into the cte that is then compared with intersect or except, as in the following pseudo code (i'm using a phone here, forgive me for not using the posted example tables)With a as (Select id , cast( randomnum as numeric(3,2) from table1), b as (Select id , cast( randomnum as numeric(3,2) from table2)Select * from aExceptSelect * from b; tom.w.brannon Ten Centuries Group: General Forum Members Points: 1328 Visits: 1264 The calculation of the column ISNULL(S.RandomNumber, 0) - ISNULL(D.RandomNumber, 0) AS RandomNumberDiffrequires some attention. It is important to choose a numeric value that is far from your threshold number or it will yeild incorrect results. For example if the RandomNumber value in one set is 0 and the key is missing in the other then the difference will calculate as 0, even though they actually are not equal. If no good dummy value is apparent then logic like the following is needed:SELECT    COALESCE(S.ID, D.ID) AS ID,   S.RandomNumber AS SrcRandomNumber,   D.RandomNumber AS DestRandomNumber,   S.RandomNumber - D.RandomNumber AS RandomNumberDiff   FROM SRC S   FULL JOIN DEST D ON S.ID = D.ID      WHERE ABS(S.RandomNumber - D.RandomNumber) > @Tolerance or (S.RandomNumber is null and D.RandomNumber is not null) or (S.RandomNumber is not null and D.RandomNumber is null); Carla Wilson-484785 SSCommitted Group: General Forum Members Points: 1715 Visits: 1949 tom.w.brannon (4/23/2013)The calculation of the column ISNULL(S.RandomNumber, 0) - ISNULL(D.RandomNumber, 0) AS RandomNumberDiffrequires some attention. It is important to choose a numeric value that is far from your threshold number or it will yeild incorrect results. For example if the RandomNumber value in one set is 0 and the key is missing in the other then the difference will calculate as 0, even though they actually are not equal. If no good dummy value is apparent then logic like the following is needed:SELECT    COALESCE(S.ID, D.ID) AS ID,   S.RandomNumber AS SrcRandomNumber,   D.RandomNumber AS DestRandomNumber,   S.RandomNumber - D.RandomNumber AS RandomNumberDiff   FROM SRC S   FULL JOIN DEST D ON S.ID = D.ID      WHERE ABS(S.RandomNumber - D.RandomNumber) > @Tolerance or (S.RandomNumber is null and D.RandomNumber is not null) or (S.RandomNumber is not null and D.RandomNumber is null);+1 I actually prefer retaining the nulls, and testing for those conditions as well. lunds007 Forum Newbie Group: General Forum Members Points: 3 Visits: 18 EXCEPT Shouldn't really be used when comparing tables with lots of columns. Investigating which fields are actually different will take more time than using the FULL OUTER JOIN with Where clause to filter on only non matched records. If your compare query has an added column to tell you which column(s) are not matching, then you won't spend time investigating which fields are different.