﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Mark  Murphy  / Using FULL JOINs to Compare Datasets / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 19 Jun 2013 02:21:47 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>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.</description><pubDate>Wed, 01 May 2013 15:48:08 GMT</pubDate><dc:creator>lunds007</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>[quote][b]tom.w.brannon (4/23/2013)[/b][hr]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) &amp;gt; @Tolerance  or (S.RandomNumber is     null and D.RandomNumber is not null)  or (S.RandomNumber is not null and D.RandomNumber is     null);[/quote]+1   I actually prefer retaining the nulls, and testing for those conditions as well.</description><pubDate>Wed, 24 Apr 2013 07:16:57 GMT</pubDate><dc:creator>Carla Wilson-484785</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>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) &amp;gt; @Tolerance  or (S.RandomNumber is     null and D.RandomNumber is not null)  or (S.RandomNumber is not null and D.RandomNumber is     null);</description><pubDate>Tue, 23 Apr 2013 11:11:24 GMT</pubDate><dc:creator>tom.w.brannon</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>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;</description><pubDate>Tue, 23 Apr 2013 03:14:29 GMT</pubDate><dc:creator>53280comma1</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>Great Article..and well laid out..</description><pubDate>Thu, 23 Dec 2010 06:59:51 GMT</pubDate><dc:creator>astmart</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>Coalesce is needed because you don't know in advance if the value will be on the source side, the destination side, or both.  If you only refer to one of the join keys, you will get nulls for the records that don't exist for that side.  Coalesce solves this problem by using the first non-null value it finds.  Does this make sense?  As an aside, you don't need coalesce for the a left join...only for a full join.</description><pubDate>Sun, 28 Nov 2010 09:24:23 GMT</pubDate><dc:creator>markoos1</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>Nice article. Thanks for your efforts for putting this together.Just wondering, what will happen if we don't use COALESCE() function? Generally when I am using LEFT JOIN to find delta at source I don't use this? Can you please elaborate?Thanks again and appriciate your work.Cheers!!</description><pubDate>Fri, 26 Nov 2010 21:08:47 GMT</pubDate><dc:creator>ashishtiwarivds</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>GPatterson  -- that's a very good point.  I've run into that exact problem before.  I was going to address that in part 2....but as a preview let me just say that the ISNULL() has to resolve to an impossible value, or else the full join will over-join. I had an ISNULL(&amp;lt;field&amp;gt;,0) once where the field had a possible value of 0, and this caused a problem.Thanks for your input.</description><pubDate>Wed, 29 Sep 2010 07:59:37 GMT</pubDate><dc:creator>markoos1</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>Thanks for the time and effort to produce this informative article. One small thing I noticed is that this solution requires an assumption that a "missing" row in either destination or source implies a 0.  Because of the way you are using ISNULL(), a particular ID that exists with a value of 0 on one side and does not exist at all on the other side will never be identified as a difference.This is probably fine in many cases (e.g. daily sales data), but it might be conceivable to create a circumstance where it would matter.  For example, if we are comparing a normalized (to z scores) data set, 0 would have a very different meaning and not be a logical default assumption.Admittedly, my example is a poor and far fetched one.  But I think this assumption is worth pointing out for those who may use this snippet of code.Geoff</description><pubDate>Wed, 29 Sep 2010 07:41:24 GMT</pubDate><dc:creator>gpatterson-825050</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>I love the articles with examples which work "out of the box".  This is one of those.  These very helpful techniques are going into my tool box.  Thank you!</description><pubDate>Tue, 28 Sep 2010 07:17:01 GMT</pubDate><dc:creator>mishaluba</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>Nice article and good examples. Thanks</description><pubDate>Mon, 27 Sep 2010 22:37:15 GMT</pubDate><dc:creator>Hardy21</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>Thank you for the great article, I am seeing some work cut out for me to improve my previously written query :-DBilly</description><pubDate>Mon, 27 Sep 2010 18:19:57 GMT</pubDate><dc:creator>le_billy</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>I'm just using the NEWID() function as a way to create unique random numbers for each row.  When you use RAND() in a query like this, it generates the same number for each row.This is just for illustration in this example.  In real situations, the numeric measures come from the source and destination queries, and aren't random numbers.</description><pubDate>Mon, 27 Sep 2010 14:06:41 GMT</pubDate><dc:creator>markoos1</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>Good article, but are you actually creating random numbers in your example, using NewID()?  Random and pseudo-random numbers have properties that this technique may not create.</description><pubDate>Mon, 27 Sep 2010 13:25:43 GMT</pubDate><dc:creator>rchantler</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>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</description><pubDate>Mon, 27 Sep 2010 11:55:05 GMT</pubDate><dc:creator>markoos1</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>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</description><pubDate>Mon, 27 Sep 2010 11:44:31 GMT</pubDate><dc:creator>shannonjk</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>Thanks for the article!</description><pubDate>Mon, 27 Sep 2010 09:45:07 GMT</pubDate><dc:creator>Trey Staker</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>Nice solution that has great utility if extended to other data types with tolerances for dates, times, text, and binary values as well.</description><pubDate>Mon, 27 Sep 2010 09:05:34 GMT</pubDate><dc:creator>sjsubscribe</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>Nicely written and detailed article.  I like the mechanism too.</description><pubDate>Mon, 27 Sep 2010 07:48:50 GMT</pubDate><dc:creator>TheSQLGuru</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>It's not so bad to maintain.  You just have to extend the WHERE clause:WHERE ABS(RandomNumberDiff) &amp;gt; @Toleranceto the following:WHERE (  ABS(RandomNumberDiff) &amp;gt; @ToleranceOR ABS(RandomNumberDiff2) &amp;gt; @ToleranceOR ABS(RandomNumberDiff3) &amp;gt; @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.</description><pubDate>Mon, 27 Sep 2010 06:40:08 GMT</pubDate><dc:creator>markoos1</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>Agreed - however if the tables have a lot of fields instead, the where clause will be long and tedious to maintain.</description><pubDate>Mon, 27 Sep 2010 06:30:05 GMT</pubDate><dc:creator>jensgc</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>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..</description><pubDate>Mon, 27 Sep 2010 03:23:29 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>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.</description><pubDate>Mon, 27 Sep 2010 00:58:17 GMT</pubDate><dc:creator>jensgc</dc:creator></item><item><title>Using FULL JOINs to Compare Datasets</title><link>http://www.sqlservercentral.com/Forums/Topic993292-2795-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/articles/FULL+JOIN/71136/"&gt;Using FULL JOINs to Compare Datasets&lt;/A&gt;[/B]</description><pubDate>Sat, 25 Sep 2010 13:16:20 GMT</pubDate><dc:creator>markoos1</dc:creator></item></channel></rss>