SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Using FULL JOINs to Compare Datasets


Using FULL JOINs to Compare Datasets

Author
Message
markoos1
markoos1
SSC-Enthusiastic
SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)

Group: General Forum Members
Points: 135 Visits: 249
Comments posted to this topic are about the item Using FULL JOINs to Compare Datasets
jensgc
jensgc
SSC Veteran
SSC Veteran (226 reputation)SSC Veteran (226 reputation)SSC Veteran (226 reputation)SSC Veteran (226 reputation)SSC Veteran (226 reputation)SSC Veteran (226 reputation)SSC Veteran (226 reputation)SSC Veteran (226 reputation)

Group: General Forum Members
Points: 226 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.
Hugo Kornelis
Hugo Kornelis
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18419 Visits: 12426
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
jensgc
jensgc
SSC Veteran
SSC Veteran (226 reputation)SSC Veteran (226 reputation)SSC Veteran (226 reputation)SSC Veteran (226 reputation)SSC Veteran (226 reputation)SSC Veteran (226 reputation)SSC Veteran (226 reputation)SSC Veteran (226 reputation)

Group: General Forum Members
Points: 226 Visits: 88
Agreed - however if the tables have a lot of fields instead, the where clause will be long and tedious to maintain.
markoos1
markoos1
SSC-Enthusiastic
SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)

Group: General Forum Members
Points: 135 Visits: 249
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.
TheSQLGuru
TheSQLGuru
SSC-Dedicated
SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)

Group: General Forum Members
Points: 31140 Visits: 8670
Nicely written and detailed article. I like the mechanism too.

Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
sjsubscribe
sjsubscribe
Mr or Mrs. 500
Mr or Mrs. 500 (545 reputation)Mr or Mrs. 500 (545 reputation)Mr or Mrs. 500 (545 reputation)Mr or Mrs. 500 (545 reputation)Mr or Mrs. 500 (545 reputation)Mr or Mrs. 500 (545 reputation)Mr or Mrs. 500 (545 reputation)Mr or Mrs. 500 (545 reputation)

Group: General Forum Members
Points: 545 Visits: 595
Nice solution that has great utility if extended to other data types with tolerances for dates, times, text, and binary values as well.
Trey Staker
Trey Staker
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1592 Visits: 2788
Thanks for the article!

---------------------------------------------------------------------
Use Full Links:
KB Article from Microsoft on how to ask a question on a Forum
shannonjk
shannonjk
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1088 Visits: 840
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 :-D

Link to my blog http://notyelf.com/
markoos1
markoos1
SSC-Enthusiastic
SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)

Group: General Forum Members
Points: 135 Visits: 249
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search