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 Saturday, September 25, 2010 1:16 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, November 3, 2014 8:17 AM
Points: 18, Visits: 210
Comments posted to this topic are about the item Using FULL JOINs to Compare Datasets
Post #993292
Posted Monday, September 27, 2010 12:58 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, May 29, 2012 2:50 AM
Points: 190, Visits: 88
The FULL OUTER JOIN method shown in the article has one major flaw compared to the EXCEPT method - identical rows are not excluded from the result set. With the example data provided it doesn't make a difference since the update ensures that there are differences in all rows - but in a real world example there will often be many identical rows and only a rather small number of differences to check (the last statement filtering out differences under a given tolerance only helps if it is possible to provide such a tolerance value - and it doesn't address the issue where a difference can occur in any number of fields.

The OUTER JOIN is still a good method to be able to compare source and destination tables - but I would combine it with the EXCEPT statement in order to only look at rows with actual differences.
Post #993465
Posted Monday, September 27, 2010 3:23 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 5:21 AM
Points: 6,098, Visits: 8,367
But you can of course add a WHERE clause to exclude the identical rows (like the final example, where small differences were excluded - change the requirement to an exact match, and you get all differences, but no matching rows..


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #993517
Posted Monday, September 27, 2010 6:30 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, May 29, 2012 2:50 AM
Points: 190, Visits: 88
Agreed - however if the tables have a lot of fields instead, the where clause will be long and tedious to maintain.
Post #993592
Posted Monday, September 27, 2010 6:40 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, November 3, 2014 8:17 AM
Points: 18, Visits: 210
It's not so bad to maintain. You just have to extend the WHERE clause:

WHERE ABS(RandomNumberDiff) > @Tolerance

to the following:

WHERE (
ABS(RandomNumberDiff) > @Tolerance
OR ABS(RandomNumberDiff2) > @Tolerance
OR ABS(RandomNumberDiff3) > @Tolerance
...
)

to get the desired result. If you had 50 columns to compare, then it would make sense to generate this code using a query off INFORMATION_SCHEMA.COLUMNS.

This is definitely a real-world technique. We're using it at my company to compare 10's of thousands of rows across over 20 columns to reconcile datasets from disparate systems.

In the next chapter of this article I'll explain why it's useful to have the exact differences like this, and not just the identification of rows that are different from source to destination.
Post #993601
Posted Monday, September 27, 2010 7:48 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 7:09 AM
Points: 4,439, Visits: 6,346
Nicely written and detailed article. I like the mechanism too.

Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
Post #993649
Posted Monday, September 27, 2010 9:05 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, December 20, 2012 1:03 PM
Points: 265, Visits: 589
Nice solution that has great utility if extended to other data types with tolerances for dates, times, text, and binary values as well.
Post #993731
Posted Monday, September 27, 2010 9:45 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, November 24, 2014 3:43 PM
Points: 1,142, Visits: 2,691
Thanks for the article!

---------------------------------------------------------------------
Use Full Links:
KB Article from Microsoft on how to ask a question on a Forum
Post #993760
Posted Monday, September 27, 2010 11:44 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: Thursday, June 19, 2014 6:36 PM
Points: 586, Visits: 822
Good article though the Except part at the beginning seems superfluous and unnecessary. If you wanted to compare data sets to see variation then I don't know why an Except query would even come to mind.

This seems like the next intuitive step to compare a data set after you have done an Except query and have determined there are records in your data set that do not match.

My nitpickyness (is that a word?) aside, this is a great article


Link to my blog http://notyelf.com/
Post #993860
Posted Monday, September 27, 2010 11:55 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, November 3, 2014 8:17 AM
Points: 18, Visits: 210
ShannonJk,

Thanks for the positive feedback, much appreciated!

I was just reviewing the EXCEPT query syntax at the top as it's the classic way to test for dataset deviations. I needed to set the context for situations where EXCEPT is appropriate before discussing where it's not.

In practice, if I were to run an EXCEPT query in some of the situations that I use the FULL JOIN, it would return many thousands of rows, with miniscule deviations. I can safely assume that the two datasets in these situations will never match exactly, which is why I go directly to the FULL JOIN technique to find the exact deviations.

-Mark


Post #993876
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse