Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««123

Using FULL JOINs to Compare Datasets Expand / Collapse
Author
Message
Posted Tuesday, April 23, 2013 3:14 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld 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 a
Except
Select * from b
;

Post #1445324
Posted Tuesday, April 23, 2013 11:11 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: 2 days ago @ 9:59 AM
Points: 568, Visits: 363
The calculation of the column
ISNULL(S.RandomNumber, 0) - ISNULL(D.RandomNumber, 0) AS RandomNumberDiff
requires 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

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, April 15, 2014 3:30 PM
Points: 1,525, Visits: 1,833
tom.w.brannon (4/23/2013)
The calculation of the column
ISNULL(S.RandomNumber, 0) - ISNULL(D.RandomNumber, 0) AS RandomNumberDiff
requires 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 01, 2013 3:48 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse