Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Using FULL JOINs to Compare Datasets Rate Topic Display Mode Topic Options
Author
 Message
 Posted Tuesday, April 23, 2013 3:14 AM
 Old Hand Group: General Forum Members Last Login: Tuesday, April 30, 2013 3:30 AM Points: 325, 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;
Post #1445324
 Posted Tuesday, April 23, 2013 11:11 AM
 Ten Centuries Group: General Forum Members Last Login: Yesterday @ 1:40 PM Points: 1,295, Visits: 1,238
 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);
Post #1445589
 Posted Wednesday, April 24, 2013 7:16 AM
 SSCommitted Group: General Forum Members Last Login: Monday, August 22, 2016 6:14 AM Points: 1,683, Visits: 1,949
 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.
Post #1445948
 Posted Wednesday, May 1, 2013 3:48 PM
 Forum Newbie Group: General Forum Members Last Login: Thursday, July 18, 2013 11:59 AM Points: 1, 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.
Post #1448635

 Permissions