<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblogcasts.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Atul Thakor </title><link>http://sqlblogcasts.com/blogs/atulthakor/default.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>Finding the maximum value/date across columns</title><link>http://sqlblogcasts.com/blogs/atulthakor/archive/2011/11/21/finding-the-maximum-value-date-across-columns.aspx</link><pubDate>Mon, 21 Nov 2011 23:01:00 GMT</pubDate><guid isPermaLink="false">fa8c4e8e-46a3-4193-8264-2c1a9cb3475d:15973</guid><dc:creator>AtulThakor</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://sqlblogcasts.com/blogs/atulthakor/rsscomments.aspx?PostID=15973</wfw:commentRss><comments>http://sqlblogcasts.com/blogs/atulthakor/archive/2011/11/21/finding-the-maximum-value-date-across-columns.aspx#comments</comments><description>&lt;p&gt;While working on some code recently I discovered a neat little trick to find the maximum value across several columns…..&lt;/p&gt;  &lt;p&gt;So the starting point was finding the maximum date across several related tables and storing the maximum value against an aggregated record.&lt;/p&gt;  &lt;p&gt;Here&amp;#39;s the sample setup code:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;USE&lt;/span&gt; TEMPDB
&lt;span class="kwrd"&gt;IF&lt;/span&gt; OBJECT_ID(&lt;span class="str"&gt;&amp;#39;CUSTOMER&amp;#39;&lt;/span&gt;) &lt;span class="kwrd"&gt;IS&lt;/span&gt; &lt;span class="kwrd"&gt;NOT&lt;/span&gt; &lt;span class="kwrd"&gt;NULL&lt;/span&gt;
&lt;span class="kwrd"&gt;BEGIN&lt;/span&gt;
    &lt;span class="kwrd"&gt;DROP&lt;/span&gt; &lt;span class="kwrd"&gt;TABLE&lt;/span&gt; CUSTOMER
&lt;span class="kwrd"&gt;END&lt;/span&gt;

&lt;span class="kwrd"&gt;IF&lt;/span&gt; OBJECT_ID(&lt;span class="str"&gt;&amp;#39;ADDRESS&amp;#39;&lt;/span&gt;) &lt;span class="kwrd"&gt;IS&lt;/span&gt; &lt;span class="kwrd"&gt;NOT&lt;/span&gt; &lt;span class="kwrd"&gt;NULL&lt;/span&gt;
&lt;span class="kwrd"&gt;BEGIN&lt;/span&gt;
    &lt;span class="kwrd"&gt;DROP&lt;/span&gt; &lt;span class="kwrd"&gt;TABLE&lt;/span&gt; ADDRESS
&lt;span class="kwrd"&gt;END&lt;/span&gt;


&lt;span class="kwrd"&gt;IF&lt;/span&gt; OBJECT_ID(&lt;span class="str"&gt;&amp;#39;ORDERS&amp;#39;&lt;/span&gt;) &lt;span class="kwrd"&gt;IS&lt;/span&gt; &lt;span class="kwrd"&gt;NOT&lt;/span&gt; &lt;span class="kwrd"&gt;NULL&lt;/span&gt;
&lt;span class="kwrd"&gt;BEGIN&lt;/span&gt;
    &lt;span class="kwrd"&gt;DROP&lt;/span&gt; &lt;span class="kwrd"&gt;TABLE&lt;/span&gt; ORDERS
&lt;span class="kwrd"&gt;END&lt;/span&gt;

&lt;span class="kwrd"&gt;SELECT&lt;/span&gt;
    1 &lt;span class="kwrd"&gt;AS&lt;/span&gt; CUSTOMERID,
    &lt;span class="str"&gt;&amp;#39;FREDDY KRUEGER&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;AS&lt;/span&gt; NAME,
    GETDATE() - 10 &lt;span class="kwrd"&gt;AS&lt;/span&gt; DATEUPDATED
&lt;span class="kwrd"&gt;INTO&lt;/span&gt;
    CUSTOMER

&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; 
    100000 &lt;span class="kwrd"&gt;AS&lt;/span&gt; ADDRESSID,
    1 &lt;span class="kwrd"&gt;AS&lt;/span&gt; CUSTOMERID,
    &lt;span class="str"&gt;&amp;#39;1428 ELM STREET&amp;#39;&lt;/span&gt; &lt;span class="kwrd"&gt;AS&lt;/span&gt; ADDRESS,
    GETDATE() -5 &lt;span class="kwrd"&gt;AS&lt;/span&gt; DATEUPDATED
&lt;span class="kwrd"&gt;INTO&lt;/span&gt;
    ADDRESS
    
&lt;span class="kwrd"&gt;SELECT&lt;/span&gt;
    123456 &lt;span class="kwrd"&gt;AS&lt;/span&gt; ORDERID,
    1 &lt;span class="kwrd"&gt;AS&lt;/span&gt; CUSTOMERID,
    GETDATE() + 1 &lt;span class="kwrd"&gt;AS&lt;/span&gt; DATEUPDATED
&lt;span class="kwrd"&gt;INTO&lt;/span&gt; ORDERS&lt;/pre&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;Now the code used a function to determine the maximum date, this performed poorly. After considering pivoting the data I opted for a case statement, this seemed reasonable until I discovered other areas which needed to determine the maximum date between 5 or more tables which didn&amp;#39;t scale well. The final solution involved using the value clause within a sub query as followed.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;SELECT&lt;/span&gt;
    C.CUSTOMERID,
    A.ADDRESSID,
    (&lt;span class="kwrd"&gt;SELECT&lt;/span&gt;
        &lt;span class="kwrd"&gt;MAX&lt;/span&gt;(DT)
    &lt;span class="kwrd"&gt;FROM&lt;/span&gt;
        (&lt;span class="kwrd"&gt;Values&lt;/span&gt;(C.DATEUPDATED),(A.DATEUPDATED),(O.DATEUPDATED)) &lt;span class="kwrd"&gt;AS&lt;/span&gt; &lt;span class="kwrd"&gt;VALUE&lt;/span&gt;(DT))
&lt;span class="kwrd"&gt;FROM&lt;/span&gt;
    CUSTOMER C
&lt;span class="kwrd"&gt;INNER&lt;/span&gt; &lt;span class="kwrd"&gt;JOIN&lt;/span&gt;
    ADDRESS A &lt;span class="kwrd"&gt;ON&lt;/span&gt; C.CUSTOMERID = A.CUSTOMERID
&lt;span class="kwrd"&gt;INNER&lt;/span&gt; &lt;span class="kwrd"&gt;JOIN&lt;/span&gt;
    ORDERS O &lt;span class="kwrd"&gt;ON&lt;/span&gt; O.CUSTOMERID = C.CUSTOMERID&lt;/pre&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

&lt;p&gt;As you can see the solution scales well and can take advantage of many of the aggregate functions!&lt;/p&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=15973" width="1" height="1"&gt;</description><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/TSQL/default.aspx">TSQL</category><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/DataWarehousing/default.aspx">DataWarehousing</category><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/Data+Warehousing/default.aspx">Data Warehousing</category><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/Performance/default.aspx">Performance</category></item><item><title>Introducing the Excel Fuzzy Matching Lookup Add-In</title><link>http://sqlblogcasts.com/blogs/atulthakor/archive/2011/05/03/introducing-the-excel-fuzzy-matching-lookup-add-in.aspx</link><pubDate>Tue, 03 May 2011 21:10:00 GMT</pubDate><guid isPermaLink="false">fa8c4e8e-46a3-4193-8264-2c1a9cb3475d:15582</guid><dc:creator>AtulThakor</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://sqlblogcasts.com/blogs/atulthakor/rsscomments.aspx?PostID=15582</wfw:commentRss><comments>http://sqlblogcasts.com/blogs/atulthakor/archive/2011/05/03/introducing-the-excel-fuzzy-matching-lookup-add-in.aspx#comments</comments><description>&lt;p&gt;My job involves a fair amount of fuzzy matching, when working on the analysis for a project I’ll often use excel as it does everything I need but its also great as you can send spreadsheets to the business knowing they’ll be comfortable using it and fairly self sufficient.&lt;/p&gt;  &lt;p&gt;This fuzzy matching Add-In from Microsoft Research really caught my eye, as it meant I could take sample records e.g.: Name1 and Name2 and run a rough estimation for how accurate the matching should be.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The setup&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Firstly you need to have Excel 2010 installed, the add-in can be downloaded from &lt;a href="http://www.microsoft.com/bi/en-us/Community/BILabs/Pages/FuzzyLookupAddInforExcel.aspx"&gt;here&lt;/a&gt;, unzip and click the usual next buttons.&lt;/p&gt;  &lt;p&gt;Here is a simple example to get you started:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Create a new spreadsheet and paste the following in A1      &lt;table cellspacing="0" cellpadding="2"&gt;         &lt;tr&gt;           &lt;td valign="top"&gt;&lt;strong&gt;New Customer&lt;/strong&gt;&lt;/td&gt;            &lt;td valign="top"&gt;&lt;strong&gt;Existing Customer&lt;/strong&gt;&lt;/td&gt;         &lt;/tr&gt;          &lt;tr&gt;           &lt;td valign="top"&gt;Jimmy Paige&lt;/td&gt;            &lt;td valign="top"&gt;Jimmy Page&lt;/td&gt;         &lt;/tr&gt;          &lt;tr&gt;           &lt;td valign="top"&gt;Ladee Gaga&lt;/td&gt;            &lt;td valign="top"&gt;Lady Gaga&lt;/td&gt;         &lt;/tr&gt;          &lt;tr&gt;           &lt;td valign="top"&gt;Jimmi Hendriks&lt;/td&gt;            &lt;td valign="top"&gt;Jimmy Hendricks&lt;/td&gt;         &lt;/tr&gt;       &lt;/table&gt;   &lt;/li&gt;    &lt;li&gt;Fuzzy lookup needs tables to compare values so…&lt;/li&gt;    &lt;li&gt;Highlight all the rows(not entire column) in column A and hit Control&amp;#160; + L this creates a table within excel for that column, ensure you tick the “My table has headers”&lt;/li&gt;    &lt;li&gt;Highlight the rows(not entire column) in column B and hit Control + L and tick the headers column, you now have the 2 tables which you are ready to compare. &lt;/li&gt;    &lt;li&gt;Your screen should look like this &lt;a href="http://sqlblogcasts.com/blogs/atulthakor/image_62BC9A07.png"&gt;&lt;br /&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://sqlblogcasts.com/blogs/atulthakor/image_thumb_7B4C3757.png" width="244" height="106" /&gt;&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;Now click the fuzzy lookup link in the toolbar and then click the fuzzy logic button, a menu should appear on the right&lt;a href="http://sqlblogcasts.com/blogs/atulthakor/image_4CF2B1AA.png"&gt;&lt;br /&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://sqlblogcasts.com/blogs/atulthakor/image_thumb_4C1A4BC0.png" width="700" height="596" /&gt;&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;At the top the 2 tables which are to be compared are chosen, left column and right column are pre-selected, in the match columns the columns to be compared have already been selected, if not click the compare button which will add them to the match columns list.&lt;/li&gt;    &lt;li&gt;You can then select the type of comparison, the default is text, other options are Zip code, Phone Number or Social Security, you can define your own by clicking configure (I’ve yet to do this)&lt;/li&gt;    &lt;li&gt;Hit ‘Go’ and you should see the following:&lt;a href="http://sqlblogcasts.com/blogs/atulthakor/image_60336849.png"&gt;&lt;br /&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://sqlblogcasts.com/blogs/atulthakor/image_thumb_239B69AC.png" width="666" height="467" /&gt;&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;As you can see we have 2 matches with similarity ratings, &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;Issues&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I’ve found the add-in to be pretty unstable ranging from running forever to not running at all, to run a new comparison I usually restart excel!&lt;/p&gt;  &lt;p&gt;Don&amp;#39;t let this put you off!&lt;/p&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=15582" width="1" height="1"&gt;</description><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/Fuzzy+Matching/default.aspx">Fuzzy Matching</category><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/Excel/default.aspx">Excel</category></item><item><title>Reporting Services 2008 R2 Gauges setting scale and colour based on value</title><link>http://sqlblogcasts.com/blogs/atulthakor/archive/2011/04/28/reporting-services-2008-r2-gauges-setting-scale-and-colour-based-on-value.aspx</link><pubDate>Thu, 28 Apr 2011 16:43:00 GMT</pubDate><guid isPermaLink="false">fa8c4e8e-46a3-4193-8264-2c1a9cb3475d:15573</guid><dc:creator>AtulThakor</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://sqlblogcasts.com/blogs/atulthakor/rsscomments.aspx?PostID=15573</wfw:commentRss><comments>http://sqlblogcasts.com/blogs/atulthakor/archive/2011/04/28/reporting-services-2008-r2-gauges-setting-scale-and-colour-based-on-value.aspx#comments</comments><description>&lt;p&gt;So…. I started to play with reporting services this week and its proving to be pretty good fun so far… here is a very quick post covering the setup of a gauge, altering its scale and changing its colour based on input values.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Firstly create a blank reporting services project&lt;/li&gt;    &lt;li&gt;Add a new Shared Data Source (for my example I’m using AdventureWorksLT2008) call this GaugeSource&lt;/li&gt;    &lt;li&gt;Add a new Shared Dataset using GaugeSource as your datasource, I’m using the following query which returns sales based on the ProductID supplied, name this GaugeData. You don&amp;#39;t need to worry about populating the @ProductID variable yet, when you run the report you’ll be prompted for a ProductID.&lt;/li&gt;    &lt;div id="codeSnippetWrapper"&gt;     &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;&lt;br /&gt;    ProductID,&lt;br /&gt;    LineTotal&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;&lt;br /&gt;    SalesLT.SalesOrderDetail&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;where&lt;/span&gt;&lt;br /&gt;    ProductID = @ProductID&lt;/pre&gt;

    &lt;br /&gt;&lt;/div&gt;

  &lt;li&gt;Add a new report (without the wizard)&lt;/li&gt;

  &lt;li&gt;Using the toolbox add a Gauge to the report and choose the default dial &lt;/li&gt;

  &lt;li&gt;When asked which dataset choose GaugeData&lt;/li&gt;

  &lt;li&gt;To change the scale right click on the Gauge-&amp;gt;Gauge Panel-&amp;gt;Scale Properties&lt;a href="http://sqlblogcasts.com/blogs/atulthakor/image_0E8CF4F3.png"&gt;&lt;br /&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://sqlblogcasts.com/blogs/atulthakor/image_thumb_5C292173.png" width="512" height="356" /&gt;&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;I set the maximum to 40,000 because I know that the maximum value is 37,000, but change this as per your data&lt;/li&gt;

  &lt;li&gt;Now you need to specify the value you wish to indicate on the dial, on my query I care about the LineTotal, to do this click on the dial (you may need to click twice, once to focus on the report and then once again to click on the dial), You’ll notice that the sum function is automatically applied to the LineTotal, this is the normal behaviour when using numerical values in SSRS&lt;a href="http://sqlblogcasts.com/blogs/atulthakor/image_26B05F4E.png"&gt;&lt;br /&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://sqlblogcasts.com/blogs/atulthakor/image_thumb_2D6368D1.png" width="632" height="287" /&gt;&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;If you click preview you should see the following, you need to enter a value before the report can run , in my example I enter 969 and hit view report&lt;a href="http://sqlblogcasts.com/blogs/atulthakor/image_7F09E323.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://sqlblogcasts.com/blogs/atulthakor/image_thumb_33AA3F5F.png" width="783" height="216" /&gt;&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;Depending on your scale/data you should see something similar to this:&lt;a href="http://sqlblogcasts.com/blogs/atulthakor/image_3A5D48E2.png"&gt;&lt;br /&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://sqlblogcasts.com/blogs/atulthakor/image_thumb_39F115ED.png" width="402" height="376" /&gt;&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;Now lets set the back colour based on the value shown&lt;/li&gt;

  &lt;li&gt;Right click on the dial and click “Gauge Properties”&lt;a href="http://sqlblogcasts.com/blogs/atulthakor/image_599FEFB5.png"&gt;&lt;br /&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://sqlblogcasts.com/blogs/atulthakor/image_thumb_124A99C3.png" width="543" height="354" /&gt;&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;On the following screen click Back Fill and then flick the Fx button: &lt;a href="http://sqlblogcasts.com/blogs/atulthakor/image_2ADA3713.png"&gt;&lt;br /&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://sqlblogcasts.com/blogs/atulthakor/image_thumb_5C65A4A8.png" width="635" height="561" /&gt;&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;Paste the following into the expression window:
    &lt;div id="codeSnippetWrapper"&gt;
      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;=IIF(&lt;span style="color:#0000ff;"&gt;sum&lt;/span&gt;(Fields!LineTotal.&lt;span style="color:#0000ff;"&gt;Value&lt;/span&gt;) &amp;gt;= 30000, &amp;quot;Green&amp;quot;, &lt;br /&gt;IIF(&lt;span style="color:#0000ff;"&gt;sum&lt;/span&gt;(Fields!LineTotal.&lt;span style="color:#0000ff;"&gt;Value&lt;/span&gt;) &amp;lt;= 500, &amp;quot;Red&amp;quot;, &amp;quot;WhiteSmoke&amp;quot;))&lt;/pre&gt;

      &lt;br /&gt;The query states that if the sum of LineTotal &amp;gt;= 30000 set the colour to Green, if the sum is &amp;lt;= 500 set the colour to Red, else WhiteSmoke.

      &lt;br /&gt;&lt;/div&gt;
  &lt;/li&gt;

  &lt;li&gt;You can test various values and colours &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hope this helps!&lt;/p&gt;

&lt;div&gt;&lt;/div&gt;

&lt;ol&gt;&lt;/ol&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=15573" width="1" height="1"&gt;</description><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/Reporting+Services+2008/default.aspx">Reporting Services 2008</category><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/SSRS/default.aspx">SSRS</category></item><item><title>Running Massive Updates, Deletes, Inserts</title><link>http://sqlblogcasts.com/blogs/atulthakor/archive/2011/04/28/running-massive-updates-deletes-inserts.aspx</link><pubDate>Thu, 28 Apr 2011 11:48:00 GMT</pubDate><guid isPermaLink="false">fa8c4e8e-46a3-4193-8264-2c1a9cb3475d:15572</guid><dc:creator>AtulThakor</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://sqlblogcasts.com/blogs/atulthakor/rsscomments.aspx?PostID=15572</wfw:commentRss><comments>http://sqlblogcasts.com/blogs/atulthakor/archive/2011/04/28/running-massive-updates-deletes-inserts.aspx#comments</comments><description>&lt;p&gt;Firstly with large transactions comes large transaction logs, ensure you grow your log accordingly before running a huge transaction because autogrows can cause overhead during execution for example: why did the query run so quickly on test rather than live… the likelihood is you ran it repeatedly on your test box and it had grown already.&lt;/p&gt;  &lt;p&gt;If you plan on running a large insert you should grow the data file accordingly, not only will this improve speed but it will also ensure you have enough space for the query to run!&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Run in loops/batches&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Old way:&lt;/p&gt;  &lt;p&gt;   &lt;div&gt;     &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;&lt;span style="color:#0000ff;"&gt;Delete&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;From&lt;/span&gt;&lt;br /&gt;    MainTable&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;where&lt;/span&gt;&lt;br /&gt;    ID &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt; ID &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; DeleteList)&lt;/pre&gt;
  &lt;/div&gt;

  &lt;div&gt;&amp;#160;&lt;/div&gt;

  &lt;div&gt;Loopy way:&lt;/div&gt;

  &lt;div id="codeSnippetWrapper"&gt;
    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;&lt;span style="color:#0000ff;"&gt;declare&lt;/span&gt;&lt;br /&gt;    @&lt;span style="color:#0000ff;"&gt;min&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;,&lt;br /&gt;    @&lt;span style="color:#0000ff;"&gt;max&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;,&lt;br /&gt;    @increments &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;,&lt;br /&gt;    @Counter &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt; @&lt;span style="color:#0000ff;"&gt;min&lt;/span&gt; = (&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;MIN&lt;/span&gt;(ID) &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; DeleteList)&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt; @&lt;span style="color:#0000ff;"&gt;max&lt;/span&gt; = (&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;MAX&lt;/span&gt;(ID) &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; DeleteList)&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt; @increments = 1000&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt; @Counter = @&lt;span style="color:#0000ff;"&gt;min&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;While&lt;/span&gt;(@Counter &amp;lt; @&lt;span style="color:#0000ff;"&gt;max&lt;/span&gt;)&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;BEGIN&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;declare&lt;/span&gt; @progress &lt;span style="color:#0000ff;"&gt;varchar&lt;/span&gt;(10)&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;Delete&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;From&lt;/span&gt;&lt;br /&gt;        MainTable&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt;&lt;br /&gt;        ID &lt;span style="color:#0000ff;"&gt;between&lt;/span&gt; @Counter &lt;span style="color:#0000ff;"&gt;and&lt;/span&gt; @Counter + @increments&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;and&lt;/span&gt;&lt;br /&gt;        ID &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt; ID &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; DeleteList)&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt; @Counter = @Counter + @increments&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt; @progress = &lt;span style="color:#0000ff;"&gt;CAST&lt;/span&gt;(@counter &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;varchar&lt;/span&gt;(10))&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;RAISERROR&lt;/span&gt; (&lt;span style="color:#006080;"&gt;&amp;#39;PROGRESS           %s&amp;#39;&lt;/span&gt;, 0, 1, @progress) &lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;WITH&lt;/span&gt; NOWAIT&lt;br /&gt;END&lt;/pre&gt;

    &lt;br /&gt;&lt;/div&gt;

  &lt;div&gt;
    &lt;br /&gt;&lt;/div&gt;
So why use the loop?&lt;/p&gt;

&lt;p&gt;Firstly you are running smaller batches so you’ll only be locking subsets, this is especially important when running on a 24/7 environment, secondly you can use the with (rowlock) option if you wish, you can play around with the batch size to find the optimal update size.&lt;/p&gt;

&lt;p&gt;You should also note the following statement:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;&lt;span style="color:#0000ff;"&gt;RAISERROR&lt;/span&gt; (&lt;span style="color:#006080;"&gt;&amp;#39;PROGRESS           %s&amp;#39;&lt;/span&gt;, 0, 1, @progress) &lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;WITH&lt;/span&gt; NOWAIT&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;When you want to report progress the print statement can be a bad idea as print messages are buffered until the batch completes, the raiserror with nowait option will immediately send the message to you, when running your query if you notice a long delay in messages you are able to react immediately.&lt;/p&gt;

&lt;p&gt;I know this is a fairly back to basics post but this is something I’ve had to go back to lots of times so thought it would be worth sharing!&lt;/p&gt;

&lt;p&gt;I’ve attached the T-SQL.&lt;/p&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=15572" width="1" height="1"&gt;</description><enclosure url="http://sqlblogcasts.com/blogs/atulthakor/attachment/15572.ashx" length="690" type="application/octet-stream" /><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/T-SQL/default.aspx">T-SQL</category><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/Insert/default.aspx">Insert</category><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/Update/default.aspx">Update</category><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/Delete/default.aspx">Delete</category><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/While+Loop/default.aspx">While Loop</category></item><item><title>First time speaking at SQLBits and the laptop debacle</title><link>http://sqlblogcasts.com/blogs/atulthakor/archive/2011/04/11/first-time-speaking-at-sqlbits-and-the-laptop-debacle.aspx</link><pubDate>Mon, 11 Apr 2011 00:01:00 GMT</pubDate><guid isPermaLink="false">fa8c4e8e-46a3-4193-8264-2c1a9cb3475d:15554</guid><dc:creator>AtulThakor</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://sqlblogcasts.com/blogs/atulthakor/rsscomments.aspx?PostID=15554</wfw:commentRss><comments>http://sqlblogcasts.com/blogs/atulthakor/archive/2011/04/11/first-time-speaking-at-sqlbits-and-the-laptop-debacle.aspx#comments</comments><description>&lt;p&gt;So this weekend I was lucky enough to get the opportunity to speak at the largest SQL Server conference in the UK, I’d been attending the conference since it began back in 2007 on and off and have seen it grow from strength to strength attracting some of the best in the industry. I had wanted to speak at the conference for a while but had never managed to submit a session, Chris Testa-O&amp;#39;Neill gave me the push I needed and I submitted the session and was lucky enough to get a spot!&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;So on the day of the talk I turned up bright and early mainly down to not being able to sleep due to a dodgy nightmare where&amp;nbsp;I alt and tabbed during my demo and there was porn on my the projector, there was no chance of getting any sleep after this!&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Martin Bell had arranged for me to test out connectivity first thing to iron out any issues as I was using a new laptop which only had a HDMI connection, guess what? It didn&amp;#39;t work!!!!!, there was a moment of complete and utter dread, Steve Bradbury relentlessly tried to fix the issue with no joy, at this point Tim Kent stepped in and offered his laptop.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Working through with Tim and with a little help from Simon Sabin I was able to get my presentation ready for the big screen, various members of the SQLBits team were around and checking to make sure things were ok which was reassuring knowing help was there!&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I cant express my gratitude enough to the SQLBits team, with so much going on these guy were able to quickly get me a replacement machine, help me fix a couple of issues moving between machines and keep everything nice and calm as well as ensuring the other sessions were running smoothly.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I must admit I was completely thrown after these issues but I have have to say I feel that I definitely came out stronger having been through this, hopefully next time things will go more smoothly.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I also wanted to say a big thank you to the people who came to the presentation and hope they enjoyed it, the feedback will be much appreciated so hopefully I can take something away from this and improve for next time!&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;A big thank you to the SQLBits organisers and fusionio (and anyone I may have missed!)&amp;nbsp;for delivering an awesome conference and event!&lt;/p&gt;
&lt;p&gt;Looking forward to SQLBits 9,&lt;/p&gt;
&lt;p&gt;Atul&lt;/p&gt;
&lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/atulthakor/Scary_15C1D88B.jpg"&gt;&lt;img style="BACKGROUND-IMAGE:none;BORDER-BOTTOM:0px;BORDER-LEFT:0px;PADDING-LEFT:0px;PADDING-RIGHT:0px;DISPLAY:inline;BORDER-TOP:0px;BORDER-RIGHT:0px;PADDING-TOP:0px;" title="Scary" border="0" alt="Scary" src="http://sqlblogcasts.com/blogs/atulthakor/Scary_thumb_10DF24CF.jpg" width="431" height="289" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Picture taken from the bean bag diving championships by Carmel Gunn(Prodata)&lt;/p&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=15554" width="1" height="1"&gt;</description><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/bean+bag/default.aspx">bean bag</category><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/conference/default.aspx">conference</category><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/presenting/default.aspx">presenting</category><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/sqlbits/default.aspx">sqlbits</category></item><item><title>LINQ for SQL Developers and DBA’s</title><link>http://sqlblogcasts.com/blogs/atulthakor/archive/2011/03/08/linq-for-sql-developers-and-dba-s.aspx</link><pubDate>Tue, 08 Mar 2011 23:51:00 GMT</pubDate><guid isPermaLink="false">fa8c4e8e-46a3-4193-8264-2c1a9cb3475d:15470</guid><dc:creator>AtulThakor</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://sqlblogcasts.com/blogs/atulthakor/rsscomments.aspx?PostID=15470</wfw:commentRss><comments>http://sqlblogcasts.com/blogs/atulthakor/archive/2011/03/08/linq-for-sql-developers-and-dba-s.aspx#comments</comments><description>&lt;p&gt;Firstly I’d just like to thank the guys who organise the SQL Server User Group (Martin/Tony/Chris) and for giving me the opportunity to speak at the recent event.&lt;/p&gt;
&lt;p&gt;Sorry about the slides taking so long but here they are along with some extra information.&lt;/p&gt;
&lt;p&gt;Firstly the demo’s were all done using LINQPad 4.0 which can be downloaded here:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.linqpad.net/"&gt;http://www.linqpad.net/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;There are 2 versions 3.5/4.0&lt;/p&gt;
&lt;p&gt;With 3.5 you should be able to replicate the problem I showed where a query using a parameter which is X characters long would create a different execution plan to a query which uses a parameter which is Y characters long, otherwise I would just use 4.0&lt;/p&gt;
&lt;p&gt;The sample database used is AdventureWorksLT2008 which can be downloaded from here:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://msftdbprodsamples.codeplex.com/releases/view/37109"&gt;http://msftdbprodsamples.codeplex.com/releases/view/37109&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The scripts have been named so that you can select the appropriate way to run them i.e.: C# expression / C#statement, each script can be run individually be highlighting the query and clicking the play symbol or hitting F5.&lt;/p&gt;
&lt;p&gt;Scripts and Slides:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/atulthakor/An%20Introduction%20to%20LINQ.zip"&gt;http://sqlblogcasts.com/blogs/atulthakor/An%20Introduction%20to%20LINQ.zip&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Please don&amp;#39;t hesitate in sending any questions via email/twitter, I’ll try my best to answer your questions!&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Atul&lt;/p&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=15470" width="1" height="1"&gt;</description><enclosure url="http://sqlblogcasts.com/blogs/atulthakor/attachment/15470.ashx" length="286484" type="application/x-zip-compressed" /><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/LINQ/default.aspx">LINQ</category></item><item><title>Performance problems loading XML with SSIS, an alternative way!</title><link>http://sqlblogcasts.com/blogs/atulthakor/archive/2011/02/02/performance-problems-loading-xml-with-ssis-an-alternative-way.aspx</link><pubDate>Wed, 02 Feb 2011 00:19:00 GMT</pubDate><guid isPermaLink="false">fa8c4e8e-46a3-4193-8264-2c1a9cb3475d:15323</guid><dc:creator>AtulThakor</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://sqlblogcasts.com/blogs/atulthakor/rsscomments.aspx?PostID=15323</wfw:commentRss><comments>http://sqlblogcasts.com/blogs/atulthakor/archive/2011/02/02/performance-problems-loading-xml-with-ssis-an-alternative-way.aspx#comments</comments><description>&lt;p&gt;I recently needed to load several thousand XML files into a SQL database, I created an SSIS package which was created as followed:&lt;/p&gt;
&lt;p&gt;Using a foreach container to loop through a directory and load each file path into a variable, the “Import XML” dataflow would then load each XML file into a SQL table.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/atulthakor/image_31A0D410.png"&gt;&lt;img style="BACKGROUND-IMAGE:none;BORDER-RIGHT-WIDTH:0px;PADDING-LEFT:0px;PADDING-RIGHT:0px;DISPLAY:inline;BORDER-TOP-WIDTH:0px;BORDER-BOTTOM-WIDTH:0px;BORDER-LEFT-WIDTH:0px;PADDING-TOP:0px;" title="image" border="0" alt="image" src="http://sqlblogcasts.com/blogs/atulthakor/image_thumb_1838D0D6.png" width="173" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Running this, it took approximately 1 second to load each file which seemed a massive amount of time to parse the XML and load the data, speaking to my colleague &lt;a href="http://sqlblogcasts.com/members/MartinCroft.aspx"&gt;Martin Croft&lt;/a&gt;, he suggested the use of T-SQL Bulk Insert and OpenRowset, so we adjusted the package as followed:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/atulthakor/image_377B77A9.png"&gt;&lt;img style="BACKGROUND-IMAGE:none;BORDER-RIGHT-WIDTH:0px;PADDING-LEFT:0px;PADDING-RIGHT:0px;DISPLAY:inline;BORDER-TOP-WIDTH:0px;BORDER-BOTTOM-WIDTH:0px;BORDER-LEFT-WIDTH:0px;PADDING-TOP:0px;" title="image" border="0" alt="image" src="http://sqlblogcasts.com/blogs/atulthakor/image_thumb_0517A42A.png" width="198" height="228" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The same foreach container was used but instead the following SQL command was executed (this is an expression):&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;quot;INSERT INTO MyTable(FileDate) &lt;br /&gt;SELECT&amp;nbsp;&amp;nbsp; CAST(bulkcolumn AS XML) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM OPENROWSET( &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BULK &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;#39;&amp;quot; + @[User::CurrentFile]&amp;nbsp; + &amp;quot;&amp;#39;, &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SINGLE_BLOB ) AS x&amp;quot;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Using this method we managed to load approximately 20 records per second, much faster…for data loading!&lt;/p&gt;
&lt;p&gt;For what we wanted to achieve this was perfect but I’ll leave you with the following points when making your own decision on which solution you decide to choose!&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Openrowset Method&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Much faster to get the data into SQL &lt;/li&gt;
&lt;li&gt;You’ll need to parse or create a view over the XML data to allow the data to be more usable(another post on this!) &lt;/li&gt;
&lt;li&gt;Not able to apply validation/transformation against the data when loading it &lt;/li&gt;
&lt;li&gt;The SQL Server service account will need permission to the file &lt;/li&gt;
&lt;li&gt;No schema validation when loading files &lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;SSIS&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Slower (in our case) &lt;/li&gt;
&lt;li&gt;Schema validation &lt;/li&gt;
&lt;li&gt;Allows you to apply transformations/joins to the data &lt;/li&gt;
&lt;li&gt;Permissions should be less of a problem &lt;/li&gt;
&lt;li&gt;Data can be loaded into the final form through the package &lt;/li&gt;
&lt;li&gt;When using a schema validation errors can fail the package (I’ll do another post on this) &lt;/li&gt;&lt;/ul&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=15323" width="1" height="1"&gt;</description><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/SSIS/default.aspx">SSIS</category><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/Performance/default.aspx">Performance</category><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/XML/default.aspx">XML</category><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/foreach/default.aspx">foreach</category><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/Openrowset/default.aspx">Openrowset</category></item><item><title>T-SQL (SCD) Slowly Changing Dimension Type 2 using a merge statement</title><link>http://sqlblogcasts.com/blogs/atulthakor/archive/2011/01/10/t-sql-scd-slowly-changing-dimension-type-2-using-a-merge-statement.aspx</link><pubDate>Mon, 10 Jan 2011 23:47:59 GMT</pubDate><guid isPermaLink="false">fa8c4e8e-46a3-4193-8264-2c1a9cb3475d:15221</guid><dc:creator>AtulThakor</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://sqlblogcasts.com/blogs/atulthakor/rsscomments.aspx?PostID=15221</wfw:commentRss><comments>http://sqlblogcasts.com/blogs/atulthakor/archive/2011/01/10/t-sql-scd-slowly-changing-dimension-type-2-using-a-merge-statement.aspx#comments</comments><description>&lt;div id="codeSnippetWrapper"&gt;Working on stored procedure recently which loads records into a data warehouse I found that the existing record was being expired using an update statement followed by an insert to add the new active record.&lt;/div&gt;  &lt;p&gt;Playing around with the merge statement you can actually expire the current record and insert a new record within one clean statement.&lt;/p&gt;  &lt;p&gt;This is how the statement works, we do the normal merge statement to insert a record when there is no match, if we match the record we update the existing record by expiring it and deactivating. &lt;/p&gt;  &lt;p&gt;At the end of the merge statement we use the output statement to output the staging values for the update,&amp;#160; we wrap the whole merge statement within an insert statement and add new rows for the records which we inserted. I’ve added the full script at the bottom so you can paste it and play around.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;div id="codeSnippetWrapper"&gt;     &lt;div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;       &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum1"&gt;   1:&lt;/span&gt; INSERT &lt;span style="color:#0000ff;"&gt;INTO&lt;/span&gt; ExampleFactUpdate &lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum2"&gt;   2:&lt;/span&gt;     (PolicyID,&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum3"&gt;   3:&lt;/span&gt;     Status)&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum4"&gt;   4:&lt;/span&gt;    &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;span style="color:#008000;"&gt;-- these columns are returned from the output statement&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum5"&gt;   5:&lt;/span&gt;     PolicyID,&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum6"&gt;   6:&lt;/span&gt;     Status&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum7"&gt;   7:&lt;/span&gt;    &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; &lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum8"&gt;   8:&lt;/span&gt;    (&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum9"&gt;   9:&lt;/span&gt;     &lt;span style="color:#008000;"&gt;-- merge statement on unique id in this case Policy_ID&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum10"&gt;  10:&lt;/span&gt;        MERGE dbo.ExampleFactUpdate dp&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum11"&gt;  11:&lt;/span&gt;        &lt;span style="color:#0000ff;"&gt;USING&lt;/span&gt; dbo.ExampleStag s&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum12"&gt;  12:&lt;/span&gt;            &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; dp.PolicyID = s.PolicyID&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum13"&gt;  13:&lt;/span&gt;        &lt;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; MATCHED &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; &lt;span style="color:#008000;"&gt;-- when we cant match the record we insert a new record record and this is all that happens&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum14"&gt;  14:&lt;/span&gt;            INSERT (PolicyID,Status)&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum15"&gt;  15:&lt;/span&gt;            &lt;span style="color:#0000ff;"&gt;VALUES&lt;/span&gt; (s.PolicyID, s.Status)&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum16"&gt;  16:&lt;/span&gt;        &lt;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; MATCHED --&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; it already &lt;span style="color:#0000ff;"&gt;exists&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum17"&gt;  17:&lt;/span&gt;            &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt; ExpiryDate &lt;span style="color:#0000ff;"&gt;IS&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt;  &lt;span style="color:#008000;"&gt;-- and the Expiry Date is null&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum18"&gt;  18:&lt;/span&gt;             &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum19"&gt;  19:&lt;/span&gt;            &lt;span style="color:#0000ff;"&gt;UPDATE&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum20"&gt;  20:&lt;/span&gt;                &lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum21"&gt;  21:&lt;/span&gt;                    dp.ExpiryDate = getdate(), --we &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt; the expiry &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; the existing record&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum22"&gt;  22:&lt;/span&gt;                    dp.Active = 0 &lt;span style="color:#008000;"&gt;-- and deactivate the existing record&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum23"&gt;  23:&lt;/span&gt;        &lt;span style="color:#0000ff;"&gt;OUTPUT&lt;/span&gt; $&lt;span style="color:#0000ff;"&gt;Action&lt;/span&gt; MergeAction, s.PolicyID, s.Status &lt;span style="color:#008000;"&gt;-- the output statement returns a merge action which can &lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum24"&gt;  24:&lt;/span&gt;    ) MergeOutput &lt;span style="color:#008000;"&gt;-- be insert/update/delete, on our example where a record has been updated (or expired in our case&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum25"&gt;  25:&lt;/span&gt;    &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt; &lt;span style="color:#008000;"&gt;-- we&amp;#39;ll filter using a where clause&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum26"&gt;  26:&lt;/span&gt;        MergeAction = &lt;span style="color:#006080;"&gt;&amp;#39;Update&amp;#39;&lt;/span&gt;; -- here&lt;/pre&gt;
&lt;/div&gt;
  &lt;/div&gt;

  &lt;div&gt;&amp;#160;&lt;/div&gt;
&lt;/div&gt;

&lt;pre class="csharpcode"&gt;&lt;strong&gt;&lt;font face="Calibri"&gt;Complete source for example&lt;/font&gt;&lt;/strong&gt;&lt;/pre&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;div id="codeSnippetWrapper"&gt;
    &lt;div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;
      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; OBJECT_ID(&lt;span style="color:#006080;"&gt;&amp;#39;ExampleFactUpdate&amp;#39;&lt;/span&gt;) &amp;gt; 0&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum2"&gt;   2:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;drop&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;table&lt;/span&gt; ExampleFactUpdate&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum3"&gt;   3:&lt;/span&gt;&amp;#160; &lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum4"&gt;   4:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Table&lt;/span&gt; ExampleFactUpdate(&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum5"&gt;   5:&lt;/span&gt;     ID &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;identity&lt;/span&gt;(1,1),   3: &lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum6"&gt;   6:&lt;/span&gt;     PolicyID &lt;span style="color:#0000ff;"&gt;varchar&lt;/span&gt;(100),&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum7"&gt;   7:&lt;/span&gt;     Status    &lt;span style="color:#0000ff;"&gt;varchar&lt;/span&gt;(100),&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum8"&gt;   8:&lt;/span&gt;     EffectiveDate datetime &lt;span style="color:#0000ff;"&gt;default&lt;/span&gt; getdate(),&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum9"&gt;   9:&lt;/span&gt;     ExpiryDate datetime,&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum10"&gt;  10:&lt;/span&gt;     Active &lt;span style="color:#0000ff;"&gt;bit&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;default&lt;/span&gt; 1&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum11"&gt;  11:&lt;/span&gt; )&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum12"&gt;  12:&lt;/span&gt;&amp;#160; &lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum13"&gt;  13:&lt;/span&gt;&amp;#160; &lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum14"&gt;  14:&lt;/span&gt; insert &lt;span style="color:#0000ff;"&gt;into&lt;/span&gt; ExampleFactUpdate(&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum15"&gt;  15:&lt;/span&gt;     PolicyID,&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum16"&gt;  16:&lt;/span&gt;     Status)&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum17"&gt;  17:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum18"&gt;  18:&lt;/span&gt;     1,&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum19"&gt;  19:&lt;/span&gt;     &lt;span style="color:#006080;"&gt;&amp;#39;Live&amp;#39;&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum20"&gt;  20:&lt;/span&gt;&amp;#160; &lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum21"&gt;  21:&lt;/span&gt; &lt;span style="color:#008000;"&gt;/*Create Staging Table*/&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum22"&gt;  22:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; OBJECT_ID(&lt;span style="color:#006080;"&gt;&amp;#39;ExampleStag&amp;#39;&lt;/span&gt;) &amp;gt; 0&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum23"&gt;  23:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;drop&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;table&lt;/span&gt; ExampleStag&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum24"&gt;  24:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum25"&gt;  25:&lt;/span&gt;&amp;#160; &lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum26"&gt;  26:&lt;/span&gt; &lt;span style="color:#008000;"&gt;/*Create example fact table */&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum27"&gt;  27:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Table&lt;/span&gt; ExampleStag(&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum28"&gt;  28:&lt;/span&gt;     PolicyID &lt;span style="color:#0000ff;"&gt;varchar&lt;/span&gt;(100),&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum29"&gt;  29:&lt;/span&gt;     Status    &lt;span style="color:#0000ff;"&gt;varchar&lt;/span&gt;(100))&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum30"&gt;  30:&lt;/span&gt;&amp;#160; &lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum31"&gt;  31:&lt;/span&gt; --&lt;span style="color:#0000ff;"&gt;add&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;some&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;data&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum32"&gt;  32:&lt;/span&gt; insert &lt;span style="color:#0000ff;"&gt;into&lt;/span&gt; ExampleStag(&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum33"&gt;  33:&lt;/span&gt;     PolicyID,&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum34"&gt;  34:&lt;/span&gt;     Status)&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum35"&gt;  35:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum36"&gt;  36:&lt;/span&gt;     1,&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum37"&gt;  37:&lt;/span&gt;     &lt;span style="color:#006080;"&gt;&amp;#39;Lapsed&amp;#39;&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum38"&gt;  38:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;union&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;all&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum39"&gt;  39:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum40"&gt;  40:&lt;/span&gt;     2,&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum41"&gt;  41:&lt;/span&gt;     &lt;span style="color:#006080;"&gt;&amp;#39;Quote&amp;#39;&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum42"&gt;  42:&lt;/span&gt;&amp;#160; &lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum43"&gt;  43:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;    *&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum44"&gt;  44:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;    ExampleFactUpdate&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum45"&gt;  45:&lt;/span&gt;&amp;#160; &lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum46"&gt;  46:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;    *&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum47"&gt;  47:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;    ExampleStag&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum48"&gt;  48:&lt;/span&gt;&amp;#160; &lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum49"&gt;  49:&lt;/span&gt;&amp;#160; &lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum50"&gt;  50:&lt;/span&gt; INSERT &lt;span style="color:#0000ff;"&gt;INTO&lt;/span&gt; ExampleFactUpdate &lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum51"&gt;  51:&lt;/span&gt;     (PolicyID,&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum52"&gt;  52:&lt;/span&gt;     Status)&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum53"&gt;  53:&lt;/span&gt;    &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;span style="color:#008000;"&gt;-- these columns are returned from the output statement&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum54"&gt;  54:&lt;/span&gt;     PolicyID,&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum55"&gt;  55:&lt;/span&gt;     Status&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum56"&gt;  56:&lt;/span&gt;    &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; &lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum57"&gt;  57:&lt;/span&gt;    (&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum58"&gt;  58:&lt;/span&gt;     &lt;span style="color:#008000;"&gt;-- merge statement on unique id in this case Policy_ID&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum59"&gt;  59:&lt;/span&gt;        MERGE dbo.ExampleFactUpdate dp&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum60"&gt;  60:&lt;/span&gt;        &lt;span style="color:#0000ff;"&gt;USING&lt;/span&gt; dbo.ExampleStag s&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum61"&gt;  61:&lt;/span&gt;            &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; dp.PolicyID = s.PolicyID&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum62"&gt;  62:&lt;/span&gt;        &lt;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; MATCHED &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; &lt;span style="color:#008000;"&gt;-- when we cant match the record we insert a new record record and this is all that happens&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum63"&gt;  63:&lt;/span&gt;            INSERT (PolicyID,Status)&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum64"&gt;  64:&lt;/span&gt;            &lt;span style="color:#0000ff;"&gt;VALUES&lt;/span&gt; (s.PolicyID, s.Status)&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum65"&gt;  65:&lt;/span&gt;        &lt;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; MATCHED --&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; it already &lt;span style="color:#0000ff;"&gt;exists&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum66"&gt;  66:&lt;/span&gt;            &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt; ExpiryDate &lt;span style="color:#0000ff;"&gt;IS&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt;  &lt;span style="color:#008000;"&gt;-- and the Expiry Date is null&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum67"&gt;  67:&lt;/span&gt;             &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum68"&gt;  68:&lt;/span&gt;            &lt;span style="color:#0000ff;"&gt;UPDATE&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum69"&gt;  69:&lt;/span&gt;                &lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum70"&gt;  70:&lt;/span&gt;                    dp.ExpiryDate = getdate(), --we &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt; the expiry &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; the existing record&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum71"&gt;  71:&lt;/span&gt;                    dp.Active = 0 &lt;span style="color:#008000;"&gt;-- and deactivate the existing record&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum72"&gt;  72:&lt;/span&gt;        &lt;span style="color:#0000ff;"&gt;OUTPUT&lt;/span&gt; $&lt;span style="color:#0000ff;"&gt;Action&lt;/span&gt; MergeAction, s.PolicyID, s.Status &lt;span style="color:#008000;"&gt;-- the output statement returns a merge action which can &lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum73"&gt;  73:&lt;/span&gt;    ) MergeOutput &lt;span style="color:#008000;"&gt;-- be insert/update/delete, on our example where a record has been updated (or expired in our case&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum74"&gt;  74:&lt;/span&gt;    &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt; &lt;span style="color:#008000;"&gt;-- we&amp;#39;ll filter using a where clause&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum75"&gt;  75:&lt;/span&gt;        MergeAction = &lt;span style="color:#006080;"&gt;&amp;#39;Update&amp;#39;&lt;/span&gt;; &lt;span style="color:#008000;"&gt;-- here&lt;/span&gt;&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum76"&gt;  76:&lt;/span&gt;&amp;#160; &lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum77"&gt;  77:&lt;/span&gt;&amp;#160; &lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum78"&gt;  78:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;    *&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum79"&gt;  79:&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;    ExampleFactUpdate&lt;/pre&gt;


      &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#606060;" id="lnum80"&gt;  80:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;/div&gt;
  &lt;/div&gt;

  &lt;div&gt;&lt;/div&gt;
&lt;/div&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=15221" width="1" height="1"&gt;</description><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/Merge/default.aspx">Merge</category><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/SCD/default.aspx">SCD</category><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/T-SQL/default.aspx">T-SQL</category><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/Data+Warehousing/default.aspx">Data Warehousing</category></item><item><title>Multiple Phone Number Matching</title><link>http://sqlblogcasts.com/blogs/atulthakor/archive/2010/10/26/telephone-number-problem.aspx</link><pubDate>Mon, 25 Oct 2010 23:28:00 GMT</pubDate><guid isPermaLink="false">fa8c4e8e-46a3-4193-8264-2c1a9cb3475d:14964</guid><dc:creator>AtulThakor</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://sqlblogcasts.com/blogs/atulthakor/rsscomments.aspx?PostID=14964</wfw:commentRss><comments>http://sqlblogcasts.com/blogs/atulthakor/archive/2010/10/26/telephone-number-problem.aspx#comments</comments><description>&lt;p&gt;Hi All, &lt;/p&gt;
&lt;p&gt;So here goes my first post (Thank you Simon with resolving the initial issues I had!).&lt;/p&gt;
&lt;p&gt;I will be eternally grateful to anyone who knows how to paste T-SQL directly into the blog without needing to manually remove the double line spacing!&lt;br /&gt;&lt;br /&gt;I recently did some work involving matching records based on a telephone numbers to find duplicates! &lt;br /&gt;&lt;br /&gt;Here&amp;#39;s a brief of the problem with my solution but I would be really curious to know if you guys have any alternative solutions for this problem.&lt;br /&gt;&lt;br /&gt;You&amp;nbsp;start with&amp;nbsp;a table which contains the following:&lt;br /&gt;&lt;/p&gt;
&lt;table class="" style="WIDTH:294px;HEIGHT:122px;" cellspacing="1" cellpadding="7"&gt;

&lt;tr&gt;
&lt;td class="" valign="bottom" height="20"&gt;&lt;font face="Calibri" size="3"&gt;&lt;font face="Calibri" size="3"&gt;&lt;span&gt;
&lt;p dir="ltr" align="left"&gt;&lt;font size="2"&gt;ID&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" valign="bottom" height="20"&gt;&lt;font face="Calibri"&gt;&lt;font face="Calibri"&gt;&lt;span&gt;
&lt;p dir="ltr" align="left"&gt;&lt;font size="2"&gt;PhoneNo1&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" valign="bottom" height="20"&gt;&lt;font face="Calibri"&gt;&lt;font face="Calibri"&gt;&lt;span&gt;
&lt;p dir="ltr" align="left"&gt;&lt;font size="2"&gt;PhoneNo2 &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" valign="bottom" height="20"&gt;&lt;font face="Calibri"&gt;&lt;font face="Calibri"&gt;&lt;span&gt;
&lt;p dir="ltr" align="left"&gt;&lt;font size="2"&gt;PhoneNo3&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td class="" valign="bottom" height="20"&gt;&lt;font face="Calibri"&gt;&lt;font face="Calibri"&gt;&lt;span&gt;
&lt;p dir="ltr" align="right"&gt;&lt;font size="2"&gt;1&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" valign="bottom" height="20"&gt;&lt;font face="Calibri"&gt;&lt;font face="Calibri"&gt;&lt;span&gt;
&lt;p dir="ltr" align="left"&gt;&lt;font size="2"&gt;00001&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" valign="bottom" height="20"&gt;&lt;font face="Calibri"&gt;&lt;font face="Calibri"&gt;&lt;span&gt;
&lt;p dir="ltr" align="left"&gt;&lt;font size="2"&gt;00002&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" valign="bottom" height="20"&gt;&lt;font face="Calibri"&gt;&lt;font face="Calibri"&gt;&lt;span&gt;
&lt;p dir="ltr" align="left"&gt;&lt;font size="2"&gt;00003&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td class="" valign="bottom" height="20"&gt;&lt;font face="Calibri"&gt;&lt;font face="Calibri"&gt;&lt;span&gt;
&lt;p dir="ltr" align="right"&gt;&lt;font size="2"&gt;2&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" valign="bottom" height="20"&gt;&lt;font face="Calibri"&gt;&lt;font face="Calibri"&gt;&lt;span&gt;
&lt;p dir="ltr" align="left"&gt;&lt;font size="2"&gt;00004&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" valign="bottom" height="20"&gt;&lt;font face="Calibri"&gt;&lt;font face="Calibri"&gt;&lt;span&gt;
&lt;p dir="ltr" align="left"&gt;&lt;font size="2"&gt;00005&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" valign="bottom" height="20"&gt;&lt;font face="Calibri"&gt;&lt;font face="Calibri"&gt;&lt;span&gt;
&lt;p dir="ltr" align="left"&gt;&lt;font size="2"&gt;00006&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td class="" valign="bottom" height="20"&gt;&lt;font face="Calibri"&gt;&lt;font face="Calibri"&gt;&lt;span&gt;
&lt;p dir="ltr" align="right"&gt;&lt;font size="2"&gt;3&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" valign="bottom" height="20"&gt;&lt;font face="Calibri"&gt;&lt;font face="Calibri"&gt;&lt;span&gt;
&lt;p dir="ltr" align="left"&gt;&lt;font size="2"&gt;00007&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" valign="bottom" height="20"&gt;&lt;font face="Calibri"&gt;&lt;font face="Calibri"&gt;&lt;span&gt;
&lt;p dir="ltr" align="left"&gt;&lt;font size="2"&gt;00001&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class="" valign="bottom" height="20"&gt;&lt;font face="Calibri"&gt;&lt;font face="Calibri"&gt;&lt;span&gt;
&lt;p dir="ltr" align="left"&gt;&lt;font size="2"&gt;00001&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;p&gt;&lt;span&gt;The aim is to compare all records to find matches on phone numbers where any phone number for a record could match any of the phone numbers on another record.&amp;nbsp;So for example ID&amp;#39;s 1 and 3 will match because Phone No &amp;#39;00001&amp;#39; is PhoneNo1 on ID 1 and PhoneNo2 on ID 3.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Initially I&amp;nbsp;tried things like cross joins which didnt work out, the final solution used UnPivot to get the data into a single list which I was able to work with to find duplicates and remove them before identifying matches. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;You should be able to paste the code and run it without any issues (skip the drop&amp;#39;s on the first run),&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Cheers&lt;/span&gt;&lt;/p&gt;&lt;span&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;
&lt;p dir="ltr" align="left"&gt;drop&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;table&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; Numbers&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;drop&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;table&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; Numbers_Unpivot&lt;/p&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;
&lt;p dir="ltr" align="left"&gt;Create&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;Table&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; Numbers&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;(&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;ID &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;int&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;identity&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;(&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;1&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;1&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;),&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;PhoneNo1 &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;varchar&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;(&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;10&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;),&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;PhoneNo2 &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;varchar&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;(&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;10&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;),&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;PhoneNo3 &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;varchar&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;(&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;10&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;)&lt;br /&gt;)&lt;/p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;
&lt;p dir="ltr" align="left"&gt;insert&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;into&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; Numbers&lt;/p&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;select&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&amp;#39;00001&amp;#39;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&amp;#39;00002&amp;#39;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&amp;#39;00003&amp;#39;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;UNION&lt;br /&gt;select&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&amp;#39;00004&amp;#39;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&amp;#39;00005&amp;#39;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&amp;#39;00006&amp;#39;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;UNION&lt;br /&gt;select&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&amp;#39;00007&amp;#39;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&amp;#39;00001&amp;#39;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&amp;#39;00001&amp;#39;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt;&lt;font face="Courier New" color="#ff0000" size="2"&gt; 
&lt;p dir="ltr" align="left"&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;Create&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;Table&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; Numbers_UnPivot&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;(&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;ID &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;int&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;,&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;PhoneNo &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;varchar&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;(&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;10&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;),&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;PhoneType &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;varchar&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;(&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;10&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;)&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;)&lt;/p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#008000" size="2"&gt;&lt;font face="Courier New" color="#008000" size="2"&gt;&lt;font face="Courier New" color="#008000" size="2"&gt;
&lt;p dir="ltr" align="left"&gt;--Unpivot Records &lt;/p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;
&lt;p dir="ltr" align="left"&gt;insert&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;into&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; Numbers_UnPivot&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;SELECT&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;ID&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;,&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;Num&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;,&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;ColName&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;FROM&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;(&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;SELECT&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;&amp;nbsp;&amp;nbsp;ID&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;,&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;&amp;nbsp; PhoneNo1&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;,&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;&amp;nbsp; PhoneNo2&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;,&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;&amp;nbsp; PhoneNo3&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;FROM&lt;br /&gt;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;Numbers&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;)&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;AS&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; piv&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;UNPIVOT&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;(&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;Num &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;FOR&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; ColName&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;IN&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;(&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;PhoneNo1&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;PhoneNo2&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;PhoneNo3&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;))&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;AS&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; s&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;go&lt;/p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#008000" size="2"&gt;&lt;font face="Courier New" color="#008000" size="2"&gt;&lt;font face="Courier New" color="#008000" size="2"&gt;--Remove Duplicates&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;with&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; CTE_Duplicates&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;As&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;(&lt;br /&gt;&amp;nbsp;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;select&lt;br /&gt;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;ID&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;,&lt;br /&gt;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#ff00ff" size="2"&gt;&lt;font face="Courier New" color="#ff00ff" size="2"&gt;&lt;font face="Courier New" color="#ff00ff" size="2"&gt;Row_Number&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;()&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;OVER&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;(&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;PARTITION&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;BY&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; ID&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;PhoneNo &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;order&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;by&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; ID&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;PhoneNo&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;)&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;as&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; RN&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;from&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;&amp;nbsp; Numbers_UnPivot&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;)&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt; 
&lt;p dir="ltr" align="left"&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;delete&lt;br /&gt;from&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;&amp;nbsp; CTE_Duplicates&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;where&lt;br /&gt;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;RN &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;=&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; 2&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;go&lt;/p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#008000" size="2"&gt;&lt;font face="Courier New" color="#008000" size="2"&gt;&lt;font face="Courier New" color="#008000" size="2"&gt;
&lt;p dir="ltr" align="left"&gt;--Group up the remainder&lt;/p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;
&lt;p dir="ltr" align="left"&gt;with&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; CTE_Multiples&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;As&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;(&lt;br /&gt;&amp;nbsp;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;select&lt;br /&gt;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;PhoneNo&lt;br /&gt;&amp;nbsp;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;from&lt;br /&gt;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;Numbers_UnPivot&lt;br /&gt;&amp;nbsp;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;group&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;by&lt;br /&gt;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;PhoneNo&lt;br /&gt;&amp;nbsp;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;having&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#ff00ff" size="2"&gt;&lt;font face="Courier New" color="#ff00ff" size="2"&gt;&lt;font face="Courier New" color="#ff00ff" size="2"&gt;COUNT&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;(*)&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&amp;gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; 1&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;)&lt;/p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;
&lt;p dir="ltr" align="left"&gt;select&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;*&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;from&lt;br /&gt;&amp;nbsp;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;Numbers_UnPivot&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;where&lt;br /&gt;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;PhoneNo &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;in(&lt;br /&gt;&amp;nbsp;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;Select&lt;br /&gt;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;PhoneNo&lt;br /&gt;&amp;nbsp;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;&lt;font face="Courier New" color="#0000ff" size="2"&gt;from&lt;br /&gt;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;font face="Courier New" size="2"&gt;CTE_Multiples&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;&lt;font face="Courier New" color="#808080" size="2"&gt;)&lt;/p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font face="Zurich BT,Trebuchet MS" size="2"&gt;&lt;font face="Zurich BT,Trebuchet MS" size="2"&gt;&lt;span&gt;
&lt;p dir="ltr" align="left"&gt;&amp;nbsp;&lt;/p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=14964" width="1" height="1"&gt;</description><category domain="http://sqlblogcasts.com/blogs/atulthakor/archive/tags/UnPivot+Over_28002900_+Partition+Row_5F00_Number_28002900_/default.aspx">UnPivot Over() Partition Row_Number()</category></item></channel></rss>