<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Cold Logics</title>
	<atom:link href="http://coldlogics.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://coldlogics.wordpress.com</link>
	<description>Where Mathematica and SQL Server converge</description>
	<lastBuildDate>Wed, 10 Apr 2013 16:27:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='coldlogics.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Cold Logics</title>
		<link>http://coldlogics.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://coldlogics.wordpress.com/osd.xml" title="Cold Logics" />
	<atom:link rel='hub' href='http://coldlogics.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Check Constraints in SQL Server and First Order Logic</title>
		<link>http://coldlogics.wordpress.com/2013/03/04/check-constraints-in-sql-server-and-first-order-logic/</link>
		<comments>http://coldlogics.wordpress.com/2013/03/04/check-constraints-in-sql-server-and-first-order-logic/#comments</comments>
		<pubDate>Mon, 04 Mar 2013 17:00:13 +0000</pubDate>
		<dc:creator>phouseak</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Check Constraint]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Database design]]></category>
		<category><![CDATA[Microsoft SQL Server]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[UDF]]></category>

		<guid isPermaLink="false">http://coldlogics.wordpress.com/?p=428</guid>
		<description><![CDATA[I have to give credit where credit is due, this post is inspired largely by a chapter in C.J. Date&#8216;s book, &#8220;SQL and Relational Theory: How to Write Accurate SQL Code&#8220;. I would recommend the book to any database professional. Domain integrity and its enforcement is a crucial component of good database design. One of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coldlogics.wordpress.com&#038;blog=17292051&#038;post=428&#038;subd=coldlogics&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><span style="color:black;">I have to give credit where credit is due, this post is inspired largely by a chapter in <a class="zem_slink" title="Christopher J. Date" href="http://en.wikipedia.org/wiki/Christopher_J._Date" target="_blank" rel="wikipedia">C.J. Date</a>&#8216;s book, &#8220;<a href="http://www.amazon.com/SQL-Relational-Theory-Write-Accurate/dp/1449316409">SQL and Relational Theory: How to Write Accurate SQL Code</a>&#8220;. I would recommend the book to any <a class="zem_slink" title="Database" href="http://en.wikipedia.org/wiki/Database" target="_blank" rel="wikipedia">database</a> professional. <a href="http://msdn.microsoft.com/en-us/library/aa933058(v=SQL.80).aspx">Domain integrity</a> and its enforcement is a crucial component of good <a class="zem_slink" title="Database design" href="http://en.wikipedia.org/wiki/Database_design" target="_blank" rel="wikipedia">database design</a>. One of the tools SQL Server includes to help enforce domain integrity is <a href="http://msdn.microsoft.com/en-US/library/ms188258(v=SQL.90).aspx">check constraints</a>. Check constraints help play an important role in database design because they can extend domain enforcement beyond mere <a href="http://msdn.microsoft.com/en-US/library/ms187752%28v=SQL.90%29.aspx">primitives</a>. This is particularly important because effective database design incorporates the enforcement of <a class="zem_slink" title="Business rule" href="http://en.wikipedia.org/wiki/Business_rule" target="_blank" rel="wikipedia">business rules</a> into its design. For example, let&#8217;s say that you work for a bike company named (<strong>*cough*</strong>) Adventure Works. Your business has a business rule that product weight must be greater than zero. This is a fair thing because, at least in the world we operate in, weight isn&#8217;t a negative thing. In the AdventureWorks2012 database, turns out there&#8217;s example of such a thing in the Production.Product table.<br />
</span></p>
<p><img alt="" src="http://coldlogics.files.wordpress.com/2013/03/030213_1659_firstorderl1.png?w=565" /><span style="color:black;"><br />
</span></p>
<p><span style="color:black;">If we look at the check constraint, we can see a very basic definition.<br />
</span></p>
<p><img alt="" src="http://coldlogics.files.wordpress.com/2013/03/030213_1659_firstorderl2.png?w=565" /><span style="color:black;"><br />
</span></p>
<p><span style="color:black;">That&#8217;s great and all but what if businesses decided it wanted to something more complex? What if Adventure Works decided that they won&#8217;t allow bike blades to more than five pounds? I actually don&#8217;t know if this is true. In fact, I&#8217;ve always wanted to be a little bit more handy with my bike. We run a quick SELECT * against the Production.Product database and we can see that the blade has a ProductID of 316.<br />
</span></p>
<p><img alt="" src="http://coldlogics.files.wordpress.com/2013/03/030213_1659_firstorderl3.png?w=565" /><span style="color:black;"><br />
</span></p>
<p><span style="color:black;">At first blush, this may seem like an <a class="zem_slink" title="Conditional (programming)" href="http://en.wikipedia.org/wiki/Conditional_%28programming%29" target="_blank" rel="wikipedia">IF THEN statement</a> would do just the trick. That, if we were to modify the <a class="zem_slink" title="Check Constraint" href="http://en.wikipedia.org/wiki/Check_Constraint" target="_blank" rel="wikipedia">CHECK constraint</a> for product weight that we may wish to have something along the lines of, IF ProductID = 316 then Weight &gt; 0.00 AND Weight &lt;= 5.00.<br />
</span></p>
<p><span style="color:black;">When we look at the documentation for CHECK constraints, there doesn&#8217;t seem to be a way to incorporate IF THEN logic into a CHECK constraints. Perhaps then a user defined function or <a class="zem_slink" title="User-defined function" href="http://en.wikipedia.org/wiki/User-defined_function" target="_blank" rel="wikipedia">UDF</a> might seem to be in order. A potential UDF CHECK constraint might look like the following:<br />
</span></p>
<p><img alt="" src="http://coldlogics.files.wordpress.com/2013/03/030213_1659_firstorderl4.png?w=565" /><span style="color:black;"><br />
</span></p>
<p><span style="color:black;">We add the constraint to the table (I dropped the previous check constraint for pedagogical purposes):<br />
</span></p>
<p><img alt="" src="http://coldlogics.files.wordpress.com/2013/03/030213_1659_firstorderl5.png?w=565" /><span style="color:black;"><br />
</span></p>
<p><span style="color:black;">If we perform a simple unrestricted update such as the following:<br />
</span></p>
<p><img alt="" src="http://coldlogics.files.wordpress.com/2013/03/030213_1659_firstorderl6.png?w=565" /><span style="color:black;"><br />
</span></p>
<p><span style="color:black;">We get the following error message:<br />
</span></p>
<p><span style="color:red;">Msg 547, Level 16, State 0, Line 3<br />
</span></p>
<p><span style="color:red;">The UPDATE statement conflicted with the CHECK constraint &#8220;CK_PRODUCT_WEIGHT&#8221;. The conflict occurred in database &#8220;AdventureWorks2012&#8243;, table &#8220;Production.Product&#8221;.<br />
</span></p>
<p><span style="color:black;">The statement has been terminated. Let&#8217;s see what happens when we do something simple like perform an unrestricted update:<br />
</span></p>
<p><img alt="" src="http://coldlogics.files.wordpress.com/2013/03/030213_1659_firstorderl7.png?w=565" /><span style="color:black;"><br />
</span></p>
<p><span style="color:black;">The outcome looks like this:<br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#595959;">SQL Server parse and compile time:<br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#595959;"> <a class="zem_slink" title="CPU time" href="http://en.wikipedia.org/wiki/CPU_time" target="_blank" rel="wikipedia">CPU time</a> = 2 ms, elapsed time = 2 ms.<br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#595959;">SQL Server parse and compile time:<br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#595959;"> CPU time = 0 ms, elapsed time = 0 ms.<br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#595959;">Table &#8216;Product&#8217;. Scan count 1, logical reads 1012, physical reads 1, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.<br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#595959;"> SQL Server Execution Times:<br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#595959;"><br />
<strong> CPU time = 15 ms, elapsed time = 110 ms</strong>.<br />
</span></p>
<p><span style="color:#595959;">(504 row(s) affected)<br />
</span></p>
<p><span style="color:black;">UDFs do not have a very good <a href="http://sqlinthewild.co.za/index.php/2009/04/29/functions-io-statistics-and-the-execution-plan/">reputation</a>. And using them in CHECK constraints can lead to <a href="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2009/06/25/scalar-udfs-wrapped-in-check-constraints-are-very-slow-and-may-fail-for-multirow-updates.aspx">performance issues and unexpected results</a> as well.<br />
</span><br />
<span style="color:black;">Perhaps there is a better way. Think back to that freshman level Introduction to Logic course you took so long ago. There was a special identity for IF THEN statements. It was IF P THEN Q = NOT P OR Q. This simple logical equivalency can come in handy in a lot of places, especially where there isn&#8217;t support of IF THEN statements but there is for simple AND/OR operations. Here&#8217;s the truth table:<br />
</span></p>
<div>
<table style="border-collapse:collapse;" border="0">
<col style="width:64px;" />
<col style="width:64px;" />
<col style="width:64px;" />
<col style="width:92px;" />
<col style="width:94px;" />
<tbody valign="top">
<tr>
<td style="padding:5px;border:solid #a3a3a3 1pt;"><strong>P</strong></td>
<td style="border-top:solid #a3a3a3 1pt;border-left:none;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;"><strong>NOT P</strong></td>
<td style="border-top:solid #a3a3a3 1pt;border-left:none;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;"><strong>Q</strong></td>
<td style="border-top:solid #a3a3a3 1pt;border-left:none;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;"><strong>IF P THEN Q</strong></td>
<td style="border-top:solid #a3a3a3 1pt;border-left:none;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;"><strong>NOT P OR Q</strong></td>
</tr>
<tr>
<td style="border-top:none;border-left:solid #a3a3a3 1pt;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;">T</td>
<td style="border-top:none;border-left:none;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;">F</td>
<td style="border-top:none;border-left:none;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;">T</td>
<td style="border-top:none;border-left:none;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;">T</td>
<td style="border-top:none;border-left:none;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;">T</td>
</tr>
<tr>
<td style="border-top:none;border-left:solid #a3a3a3 1pt;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;">T</td>
<td style="border-top:none;border-left:none;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;">F</td>
<td style="border-top:none;border-left:none;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;">F</td>
<td style="border-top:none;border-left:none;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;">F</td>
<td style="border-top:none;border-left:none;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;">F</td>
</tr>
<tr>
<td style="border-top:none;border-left:solid #a3a3a3 1pt;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;">F</td>
<td style="border-top:none;border-left:none;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;">T</td>
<td style="border-top:none;border-left:none;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;">T</td>
<td style="border-top:none;border-left:none;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;">T</td>
<td style="border-top:none;border-left:none;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;">T</td>
</tr>
<tr>
<td style="border-top:none;border-left:solid #a3a3a3 1pt;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;">F</td>
<td style="border-top:none;border-left:none;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;">T</td>
<td style="border-top:none;border-left:none;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;">F</td>
<td style="border-top:none;border-left:none;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;">T</td>
<td style="border-top:none;border-left:none;border-bottom:solid #a3a3a3 1pt;border-right:solid #a3a3a3 1pt;padding:5px;">T</td>
</tr>
</tbody>
</table>
</div>
<p><span style="color:black;">Instead of looking to a UDF to perform domain integrity, we can use a simple logical disjunction instead. So, instead the CHECK constraint might read:<br />
</span></p>
<p style="margin-left:27pt;"><img alt="" src="http://coldlogics.files.wordpress.com/2013/03/030213_1659_firstorderl8.png?w=565" /><span style="color:black;"><br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#000000;">The same unrestricted query looks like this:<br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#595959;">SQL Server parse and compile time:<br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#595959;"> CPU time = 15 ms, elapsed time = 49 ms.<br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#595959;">SQL Server parse and compile time:<br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#595959;"> CPU time = 0 ms, elapsed time = 0 ms.<br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#595959;">Table &#8216;Product&#8217;. Scan count 1, logical reads 1012, physical reads 1, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.<br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#595959;"> SQL Server Execution Times:<br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#595959;"><br />
<strong>CPU time = 16 ms, elapsed time = 13 ms.</strong><br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#595959;">(504 row(s) affected)<br />
</span></p>
<p><span style="color:black;">113 ms compared to 13 ms?!? The difference between the CHECK constraint using an UDF and one using a simple NOT P OR Q is separated by almost a factor of 10. This is a simple table with a tiny amount of data, 504 rows. Imagine what this looks like at scale with millions of rows. A little bit of logic can save a lot of time. Moral of the story? Where and when you can, use logic and relational algebra, it will usually yield better database performance.<br />
</span><br />
<span style="color:black;">I know I linked to it in this blog post already but I want to call it out again, please read Gail Shaw&#8217;s post on UDFs. It&#8217;s very good: <a href="http://sqlinthewild.co.za/index.php/2009/04/29/functions-io-statistics-and-the-execution-plan/">http://sqlinthewild.co.za/index.php/2009/04/29/functions-io-statistics-and-the-execution-plan/</a>.<br />
</span><br />
<span style="color:black;">Oh, and I think a bike blade might be a type of fender. At least, that&#8217;s what my searches on REI.com tell me.<br />
</span></p>
<br />Filed under: <a href='http://coldlogics.wordpress.com/category/sql-server/'>SQL Server</a> Tagged: <a href='http://coldlogics.wordpress.com/tag/check-constraint/'>Check Constraint</a>, <a href='http://coldlogics.wordpress.com/tag/database/'>Database</a>, <a href='http://coldlogics.wordpress.com/tag/database-design/'>Database design</a>, <a href='http://coldlogics.wordpress.com/tag/microsoft-sql-server/'>Microsoft SQL Server</a>, <a href='http://coldlogics.wordpress.com/tag/sql/'>SQL</a>, <a href='http://coldlogics.wordpress.com/tag/sql-server/'>SQL Server</a>, <a href='http://coldlogics.wordpress.com/tag/udf/'>UDF</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coldlogics.wordpress.com/428/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coldlogics.wordpress.com/428/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coldlogics.wordpress.com&#038;blog=17292051&#038;post=428&#038;subd=coldlogics&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://coldlogics.wordpress.com/2013/03/04/check-constraints-in-sql-server-and-first-order-logic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0fcb3c49aad00374ad5d43b9e6a551cd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">phouseak</media:title>
		</media:content>

		<media:content url="http://coldlogics.files.wordpress.com/2013/03/030213_1659_firstorderl1.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2013/03/030213_1659_firstorderl2.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2013/03/030213_1659_firstorderl3.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2013/03/030213_1659_firstorderl4.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2013/03/030213_1659_firstorderl5.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2013/03/030213_1659_firstorderl6.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2013/03/030213_1659_firstorderl7.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2013/03/030213_1659_firstorderl8.png" medium="image" />
	</item>
		<item>
		<title>Installing SQL Server 2012</title>
		<link>http://coldlogics.wordpress.com/2012/04/10/installing-sql-server-2012/</link>
		<comments>http://coldlogics.wordpress.com/2012/04/10/installing-sql-server-2012/#comments</comments>
		<pubDate>Tue, 10 Apr 2012 19:00:38 +0000</pubDate>
		<dc:creator>phouseak</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Installation (computer programs)]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Microsoft SQL Server]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server Express]]></category>

		<guid isPermaLink="false">http://coldlogics.wordpress.com/?p=411</guid>
		<description><![CDATA[Microsoft has released their new version of SQL Server, SQL Server 2012. This guide will walk you through getting SQL Server 2012 and installing it. The first thing to do is get the software. To do this, just follow this link and click on the Download SQL Server 2012 button. If you&#8217;re on a 64-bit [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coldlogics.wordpress.com&#038;blog=17292051&#038;post=411&#038;subd=coldlogics&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Microsoft has released their new version of SQL Server, <a href="http://www.microsoft.com/sqlserver/en/us/product-info.aspx">SQL Server 2012</a>. This guide will walk you through getting SQL Server 2012 and installing it. The first thing to do is get the software. To do this, just follow this <a href="http://www.microsoft.com/sqlserver/en/us/get-sql-server/try-it.aspx">link</a> and click on the Download SQL Server 2012 button. If you&#8217;re on a 64-bit computer, you&#8217;ll want to download ENU\x64\SQLFULL_x64_ENU_Core.box, ENU\x64\SQLFULL_x64_ENU_Lang.box, and ENU\x64\SQLFULL_x64.ENU_Install.exe.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings1.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>If you&#8217;re on a 32-bit system, you&#8217;ll want the 32 bit equivalents available below these options. Once you&#8217;ve downloaded those files, find SQLFULL_x64.ENU_Install.exe and execute it.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings2.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span><br />
The software will unpack and create an install directory.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings3.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Find the install directory that the software unpacked to, named SQLFULL_x64_ENU, click inside it and double click on the bootstrapper file called SETUP.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings4.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>A window called the SQL Server Installation Center will open up. The first option is Planning. If you are not familiar with SQL Server and want to go through a few of the options there, feel free. Once you are ready to install, click on the Installation link on the left hand portion of the window. The usual selection here is select, &#8220;New SQL Server stand-alone installation or add features to an existing installation&#8221;. Select that option.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings5.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>The next window will just show the progress of SQL Server performing some sanity checking. If there&#8217;s any errors here, you&#8217;ll want to view the detailed report and see what is wrong with your setup. This isn&#8217;t that common.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings6.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Once done, click OK. The setup process will ask you for a key. If you have a key, you may enter it, otherwise you may select either &#8220;Evaluation&#8221; or the SQL Server Express option. For our purposes, just select the evaluation option and hit Next.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings7.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Accept the license terms and hit Next.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings8.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>After that, more preparation in the way of setup files will occur. Once that is done, hit Install.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings9.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>SQL Server Setup will perform some additional sanity checks to make sure there&#8217;s no problems with your computing environment. Everything should check out. If it does, hit the Next button to continue.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings10.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Now we&#8217;re getting to the point where we get to start making some choices with respect to our installation. Since we want to get the full SQL Server experience, select the first option, &#8220;SQL Server Feature Installation&#8221; and hit the Next button.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings11.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Since this is our first installation of SQL Server 2012 (that is why you&#8217;re following this guide, right?), we&#8217;ll likely want to play with all the available options. If that is the case, hit the &#8220;Select All&#8221; button below the Features window. If not, you&#8217;ll want to scan through the features and cherry pick the ones you wish to install. When you&#8217;re comfortable with your choices, hit the Next button.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings12.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>More sanity checking. If you&#8217;re curious, click, &#8220;Show details&#8221;, elsewise hit Next to continue.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings13.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>If you have more than one copy of SQL Server installed (in my case, I have both a copy of SQL Server 2008 R2 and a copy of SQL Server Express), you&#8217;ll need to create a named instance for your SQL Server 2012 installation. In this case, I imaginatively called mine SQL2012. If you&#8217;re installing SQL Server for the first time, it will make your life a lot easier if you just use the default instance.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings14.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>After you hit Next, SQL Server will want to make sure that you have enough room on disk for your installation. Review the disk usage summary and hit next if you have enough room. You should.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings15.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>SQL Server 2012 Setup will now ask you for the account names and <a href="http://msdn.microsoft.com/en-us/library/aa174903(v=SQL.80).aspx">collation</a> specification. If this is a production box, you&#8217;ll want to follow your organization&#8217;s <a href="http://technet.microsoft.com/en-us/library/cc875826.aspx">service account best practices</a>. If you&#8217;re installing this on your home computer for testing and learning purposes, you may just wish to accept the defaults here and hit Next.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings16.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>The database engine configuration options are up next. Under the Server Configuration tab, select the Authentication Mode you wish to have. I usually pick Windows authentication mode for my test installations unless I need Mixed Mode. You&#8217;ll want to add whatever accounts you wish to have administrative access to your SQL Server 2012 installation. Typically, it&#8217;s at least the account you&#8217;re installing the software from if not others as well. Once that is done, click the Data Directories tab.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings17.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Here you may change where SQL Server stores various files as well as the defaults for when you create new databases. I&#8217;m on a laptop with just once hard drive, the C:\ drive, so I&#8217;m going to keep the folders as they are. Many times on a production server, you&#8217;ll be changing these fields here. Once you&#8217;re done, click the FILESTREAM tab.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings18.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Starting with SQL Server 2008, SQL Server has been able to include unstructured data as part of a logical database structure via <a href="http://msdn.microsoft.com/en-us/library/gg471497.aspx">FILESTREAM</a>. This addresses the common situation where images or documents may be held on the filesystem while referencing paths and attributes are held within the database. Learning how to work with FILESTREAM is highly recommended, so I would suggest turning it on. Once done, click the Next button.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings19.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Since were installing <a href="http://msdn.microsoft.com/en-US/library/ms175609(v=SQL.90).aspx">SSAS</a> as well, select the mode you wish, <a href="http://technet.microsoft.com/en-us/library/ms143708(v=sql.110).aspx">Multidimensional and Data Mining Mode</a> or <a href="http://technet.microsoft.com/en-us/library/hh231722.aspx">Tabular Mode</a>. Make sure to add an account that will have administrative permissions as well, usually at least the one you&#8217;re installing from. Then click the Data Directories tab.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings20.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Here, if you wish to change where SSAS files are located and default locations for new ones, modify the paths to where you wish those files to be located. Once finished, click the Next button.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings21.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Next up is the Reporting Services Configuration window. I typically just select the defaults here, &#8220;Install and configure&#8221; for Reporting Services Native Mode and, &#8220;Install only&#8221; for Sharepoint Integrated mode. Select the option that makes sense to you and hit Next.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings22.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Next up, SQL Server 2012 Setup will ask you about how to set up the <a href="http://msdn.microsoft.com/en-us/library/ff878183.aspx">Distributed Replay</a> Controller. I have yet to explore Distributed Replay but from the documentation, you&#8217;d want to use it when:</p>
<ul>
<li>You want to evaluate application compatibility. For example, you want to test SQL Server and operating system upgrade scenarios, hardware upgrades, or index tuning.<span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></li>
<li>The concurrency in the captured trace is so high that a single replay client cannot sufficiently simulate it.</li>
</ul>
<p>Specify the users you wish to have permissions for the service and hit Next.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings23.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Next up, specify the distributed replay client. I just set up my own computer in this case since this is a stand alone installation.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings24.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Next up, select the options you wish to have for error reporting. I tend to give Microsoft access to my errors. I&#8217;m not particularly paranoid about that kind of information. Select the option that aligns best with your own personal persuasion.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings25.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>SQL Server will then wish to perform more sanity checking. If your system passes muster, click Next.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings26.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>SQL Server will give you one final review window before it installs. Review everything here and then click Install and it&#8217;s off to the races.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings27.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Walk away, take a nap, go on a walk, do something and once you&#8217;ve returned, hopefully the window below will be waiting for you. Congratulations! You&#8217;ve successfully installed SQL Server 2012. Smile to yourself and click the Close button.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings28.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Just to make sure that everything is in working order. Find your SQL Server 2012 installation and select SQL Server Management Studio.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings29.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Once it opens, it&#8217;ll ask what server you wish to connect to. Here, I type in the name of SQL Server 2012 instance name and click Connect. If you have a default instance, you can just go to localhost to connect.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings30.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Once you&#8217;ve successfully connected, you&#8217;ll have a screen like mine below sans the AdventureWorks2012 database. To get the AdventureWorks2012 database, go to the Adventures Works for SQL Server 2012 Codeplex <a href="http://msftdbprodsamples.codeplex.com/releases/view/55330">page</a>. You&#8217;ll want to attach the database file once you&#8217;ve downloaded it. If you&#8217;ve never done that before, follow the tutorial <a href="http://www.kodyaz.com/sql-server-2012/download-sample-database-adventureworks-for-sql-server-2012.aspx">here</a>.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings31.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Once you have the AdventureWorks2012 database installed, it&#8217;s time to play! If you&#8217;ve never played with SQL Server before, you may wish to get started <a href="http://coldlogics.wordpress.com/2010/11/25/early-encounters-with-data/">here</a>. Happy querying!</p>
<br />Filed under: <a href='http://coldlogics.wordpress.com/category/sql-server/'>SQL Server</a> Tagged: <a href='http://coldlogics.wordpress.com/tag/database/'>Database</a>, <a href='http://coldlogics.wordpress.com/tag/installation-computer-programs/'>Installation (computer programs)</a>, <a href='http://coldlogics.wordpress.com/tag/microsoft/'>Microsoft</a>, <a href='http://coldlogics.wordpress.com/tag/microsoft-sql-server/'>Microsoft SQL Server</a>, <a href='http://coldlogics.wordpress.com/tag/sql/'>SQL</a>, <a href='http://coldlogics.wordpress.com/tag/sql-server/'>SQL Server</a>, <a href='http://coldlogics.wordpress.com/tag/sql-server-2012/'>SQL Server 2012</a>, <a href='http://coldlogics.wordpress.com/tag/sql-server-express/'>SQL Server Express</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coldlogics.wordpress.com/411/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coldlogics.wordpress.com/411/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coldlogics.wordpress.com&#038;blog=17292051&#038;post=411&#038;subd=coldlogics&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://coldlogics.wordpress.com/2012/04/10/installing-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0fcb3c49aad00374ad5d43b9e6a551cd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">phouseak</media:title>
		</media:content>

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings1.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings2.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings3.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings4.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings5.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings6.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings7.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings8.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings9.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings10.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings11.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings12.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings13.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings14.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings15.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings16.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings17.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings18.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings19.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings20.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings21.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings22.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings23.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings24.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings25.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings26.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings27.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings28.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings29.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings30.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/04/040812_2319_installings31.png" medium="image" />
	</item>
		<item>
		<title>Building a Simple Map in SSRS 2008 R2</title>
		<link>http://coldlogics.wordpress.com/2012/03/31/building-a-simple-map-in-ssrs/</link>
		<comments>http://coldlogics.wordpress.com/2012/03/31/building-a-simple-map-in-ssrs/#comments</comments>
		<pubDate>Sat, 31 Mar 2012 23:23:54 +0000</pubDate>
		<dc:creator>phouseak</dc:creator>
				<category><![CDATA[SSRS]]></category>
		<category><![CDATA[Business Intelligence Development Studio]]></category>
		<category><![CDATA[Microsoft SQL Server]]></category>
		<category><![CDATA[SQL Server Reporting Service]]></category>

		<guid isPermaLink="false">http://coldlogics.wordpress.com/?p=376</guid>
		<description><![CDATA[In a previous post we discussed mapping the SQL Server geography data type in Mathematica. Now we&#8217;re going to build a simple map in a SSRS report using the same query as before. To start, we&#8217;ll open Visual Studio &#8211; Business Intelligence Development Studio (BIDS) and create a simple data source connection. Right click on [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coldlogics.wordpress.com&#038;blog=17292051&#038;post=376&#038;subd=coldlogics&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In a <a href="http://coldlogics.wordpress.com/2012/02/21/plotting-latitudelongitude-points-in-mathematica-using-the-sql-server-2008-geography-data-type/">previous post</a> we discussed mapping the <a class="zem_slink" title="Microsoft SQL Server" href="http://en.wikipedia.org/wiki/Microsoft_SQL_Server" rel="wikipedia" target="_blank">SQL Server</a> <a href="http://msdn.microsoft.com/en-us/library/cc280766(v=sql.105).aspx">geography data type</a> in Mathematica. Now we&#8217;re going to build a simple map in a <a class="zem_slink" title="SQL Server Reporting Services" href="http://en.wikipedia.org/wiki/SQL_Server_Reporting_Services" rel="wikipedia" target="_blank">SSRS</a> report using the same query as before. To start, we&#8217;ll open <a class="zem_slink" title="Microsoft Visual Studio" href="http://en.wikipedia.org/wiki/Microsoft_Visual_Studio" rel="wikipedia" target="_blank">Visual Studio</a> &#8211; <a class="zem_slink" title="Business Intelligence Development Studio" href="http://en.wikipedia.org/wiki/Business_Intelligence_Development_Studio" rel="wikipedia" target="_blank">Business Intelligence Development Studio</a> (BIDS) and create a simple data source connection. Right click on Shared Data Sources and click on, &#8220;Add New Data Source&#8221;.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi1.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>In the next window that pops up, name the connection (here we just called it Localhost) and then click on the Edit button.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi2.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>In the window that pops up, enter the server and the database that you would like to connect to.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi3.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>You can click the Test Connection button if you wish but really, if you can select the database, you can connect to the database. Click OK twice to create the shared data source.</p>
<p>Once that is done, we will want to create a report. As usual, you can just right-click on Reports to get the menu. If you click on, &#8220;Add New Report&#8221;, it&#8217;ll take you to the Add New Report Wizard. I prefer to just create a blank report and create from there. To do that, click on Add -&gt; New Item&#8230;</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi4.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Select the Report option and give it a name. I named mine, Top 500 Sales Around the World.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi5.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Next, click on your Toolbox pane, select the Map object, and drag it over to your report.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi6.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>When you drag it over, a new window will pop up with options for adding a new map layer. Select the option for a SQL Server spatial query and hit, &#8220;Next&#8221;.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi7.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>The next window asks if you wish to choose an existing dataset or create a new one. We will want to create a new one (it&#8217;s at the bottom). So select that option and then hit the Next button.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi8.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Click the &#8220;New…&#8221; button on the next window that pops up to create a new data source.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi9.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>On the Data Source Properties window, name the connection something sane and then select the, &#8220;Use shared data source refence&#8221; option and in the dropdown list, select the shared data source we created before. Click OK.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi10.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Then click Next.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi11.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>The next window that pops up will want you to create a query. Select, &#8220;Edit as Text&#8221; and enter the following query:</p>
<p style="margin-left:27pt;"><span style="color:#7f7f7f;"><a class="zem_slink" title="Select (SQL)" href="http://en.wikipedia.org/wiki/Select_%28SQL%29" rel="wikipedia" target="_blank">SELECT</a> TOP (500) Address.SpatialLocation<br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#7f7f7f;">FROM Person.Address AS Address <a class="zem_slink" title="Join (SQL)" href="http://en.wikipedia.org/wiki/Join_%28SQL%29" rel="wikipedia" target="_blank">INNER JOIN</a><br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#7f7f7f;"> Sales.SalesOrderHeader ON Address.AddressID = Sales.SalesOrderHeader.ShipToAddressID<br />
</span></p>
<p><img src="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi12.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Click Next. SSRS will detect that the values we are using are points. With the points themselves just being points in space, you&#8217;ll want some form of underlying map to plot them on to. At the bottom, select the option to add a Bing maps layer and click Next.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi13.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Select the type of marker map you want to use and click Next.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi14.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Select your final options such as theme, marker types, and color options and then click Finish.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi15.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Once that is done, you can click on the Preview pane and see your shiny new SSRS map!</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi16.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Please note that while my map is called Top 500 sales locations, it isn&#8217;t a top 500 in the usual sense (500 highest grossing sales). It&#8217;s merely the first 500 rows in the Person.Address table that have matching values in the Sales.SalesOrderHeader table. Building a map in SSRS is impressively easy.</p>
<br />Filed under: <a href='http://coldlogics.wordpress.com/category/ssrs/'>SSRS</a> Tagged: <a href='http://coldlogics.wordpress.com/tag/business-intelligence-development-studio/'>Business Intelligence Development Studio</a>, <a href='http://coldlogics.wordpress.com/tag/microsoft-sql-server/'>Microsoft SQL Server</a>, <a href='http://coldlogics.wordpress.com/tag/sql-server-reporting-service/'>SQL Server Reporting Service</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coldlogics.wordpress.com/376/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coldlogics.wordpress.com/376/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coldlogics.wordpress.com&#038;blog=17292051&#038;post=376&#038;subd=coldlogics&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://coldlogics.wordpress.com/2012/03/31/building-a-simple-map-in-ssrs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0fcb3c49aad00374ad5d43b9e6a551cd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">phouseak</media:title>
		</media:content>

		<media:content url="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi1.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi2.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi3.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi4.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi5.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi6.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi7.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi8.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi9.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi10.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi11.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi12.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi13.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi14.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi15.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/03/033112_2323_buildingasi16.png" medium="image" />
	</item>
		<item>
		<title>Plotting Latitude/Longitude Points in Mathematica using the SQL Server 2008 Geography Data Type</title>
		<link>http://coldlogics.wordpress.com/2012/02/21/plotting-latitudelongitude-points-in-mathematica-using-the-sql-server-2008-geography-data-type/</link>
		<comments>http://coldlogics.wordpress.com/2012/02/21/plotting-latitudelongitude-points-in-mathematica-using-the-sql-server-2008-geography-data-type/#comments</comments>
		<pubDate>Tue, 21 Feb 2012 16:33:56 +0000</pubDate>
		<dc:creator>phouseak</dc:creator>
				<category><![CDATA[Mathematica]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Geospatial]]></category>
		<category><![CDATA[Map]]></category>
		<category><![CDATA[Microsoft SQL Server]]></category>

		<guid isPermaLink="false">http://coldlogics.wordpress.com/?p=350</guid>
		<description><![CDATA[When Microsoft released SQL Server 2008, they introduced a new data type with it called the geography data type. They geography data type filled an important gap in SQL Server data types where previously stored data was usually held as latitude and longitude values in separate columns. The geography data type directly addresses this gap [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coldlogics.wordpress.com&#038;blog=17292051&#038;post=350&#038;subd=coldlogics&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>When Microsoft released <a class="zem_slink" title="Microsoft SQL Server" href="http://www.microsoft.com/sqlserver" rel="homepage">SQL Server 2008</a>, they introduced a new data type with it called the <a href="http://msdn.microsoft.com/en-us/library/bb933876.aspx">geography</a> data type. They geography data type filled an important gap in SQL Server data types where previously stored data was usually held as latitude and longitude values in separate columns. The geography data type directly addresses this gap and implements methods as <a href="http://www.opengeospatial.org/standards/sfa">specified</a> by the <a href="http://msdn.microsoft.com/en-us/library/bb933802.aspx">Open Geospatial Consortium</a>. <a class="zem_slink" title="Mathematica" href="http://www.wolfram.com/products/mathematica/" rel="homepage">Mathematica</a> also ships with a full retinue of <a href="http://reference.wolfram.com/mathematica/guide/GeospatialFormats.html">geospatial functionality</a> and has the ability work with <a href="http://en.wikipedia.org/wiki/Shapefile">shapefiles</a> and much, much more. It would a nice exercise and great learning opportunity to combine the two.</p>
<p>We would like to extract geospatial information from the <a class="zem_slink" title="Contoso" href="http://en.wikipedia.org/wiki/Contoso" rel="wikipedia">AdventureWorks</a> database and plot it in Mathematica. The AdventureWorks database ships with geographic data contained in the Person.Address table.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/02/022112_1633_plottinglat1.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span>Let&#8217;s assume that we want to extract sales information regarding where products are being shipped to from the AdventureWorks database. If we create a database diagram in SQL Server starting with the Person.Address table and add related tables, we will have a diagram similar to the one below.</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/02/022112_1633_plottinglat2.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>In this diagram, we can see that the Person.Address table and the Sales.SalesOrderHeader table are related to each other by a <a href="http://technet.microsoft.com/en-us/library/ms175464.aspx">foreign key relationship</a>.</p>
<p>With the column and referential information we have in hand, we&#8217;re ready to build a candidate query to extract this information. In this particular case, we&#8217;re only interested in plotting lat/long points and so we will use the <a href="http://msdn.microsoft.com/en-us/library/bb933806.aspx">Lat</a> and <a href="http://msdn.microsoft.com/en-us/library/bb933958.aspx">Long</a> from <a href="http://msdn.microsoft.com/en-us/library/bb933968.aspx">Microsoft&#8217;s extended methods</a> for the geography data type.</p>
<p>The query itself is a simple inner join pulling lats and longs from the Person.Address table and joining them on the SalesOrderHeader table.</p>
<p style="margin-left:27pt;"><span style="color:#7f7f7f;">SELECT DISTINCT TOP 500 SpatialLocation.Lat AS Lat, SpatialLocation.Long AS Long</span></p>
<p style="margin-left:27pt;"><span style="color:#7f7f7f;">FROM Person.Address <a class="zem_slink" title="Join (SQL)" href="http://en.wikipedia.org/wiki/Join_%28SQL%29" rel="wikipedia">INNER JOIN</a> Sales.SalesOrderHeader</span></p>
<p style="margin-left:27pt;"><span style="color:#7f7f7f;">ON Person.Address.AddressID = SalesOrderHeader.ShipToAddressID<br />
</span></p>
<p>The output below should be what you get:</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/02/022112_1633_plottinglat3.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>With this data in hand, it&#8217;s time to move over to Mathematica. We will want to connect to <a href="http://coldlogics.wordpress.com/2011/01/24/connecting-to-sql-part-ii/">SQL Server using Database Link</a>. Once that is done, we will want to set up a <a href="http://reference.wolfram.com/mathematica/DatabaseLink/tutorial/SQLExecute.html">SQLExecute</a> to use the connection we have built and execute the query above. My Mathematica notebook looks like the following:</p>
<p><img src="http://coldlogics.files.wordpress.com/2012/02/022112_1633_plottinglat4.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span>Next, we will need to use <a href="http://reference.wolfram.com/mathematica/ref/GeoGridPosition.html">GeoGridPosition</a> and <a href="http://reference.wolfram.com/mathematica/ref/GeoPosition.html">GeoPosition</a> to graph the points on a map. To do that, we will use the following:</p>
<p style="margin-left:27pt;"><span style="color:#7f7f7f;">Graphics[{Red,Point/@Map[GeoGridPosition[GeoPosition[#],&#8221;Mercator&#8221;][[1]]&amp;,{spatialquery},{2}],Gray,Polygon[Map[GeoGridPosition[GeoPosition[#],&#8221;Mercator&#8221;][[1]]&amp;,CountryData[#,"Coordinates"],{2}]]&amp;/@CountryData["Countries"]}]<br />
</span></p>
<p>The output should then be as follows (click picture for larger size):</p>
<p><a href="http://coldlogics.files.wordpress.com/2012/02/adventuresworkssalesmap.png"><img src="http://coldlogics.files.wordpress.com/2012/02/022112_1633_plottinglat5.png?w=565" alt="" /></a><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span>It looks like most of our sales are going to the west coast of the United States, the east coast of England and the east coast of Australia.</p>
<p>As a side note, this post couldn&#8217;t be possible without the very helpful users at the <a href="http://mathematica.stackexchange.com/">Mathematica StackExchange</a>. I&#8217;ve never worked with geospatial data in Mathematica or SQL Server before and I wanted to learn about how to do this in Mathematica and found myself without the ability to chart a direction. I posted a question and within hours the answers were all pouring in. It&#8217;s a pity I can only accept one reply as the answer. The Mathematica code I&#8217;m using in this post is borrowed from the accepted solution to my question <a href="http://mathematica.stackexchange.com/questions/2076/need-general-assistance-with-plotting-lat-long-values">here</a>.</p>
<br />Filed under: <a href='http://coldlogics.wordpress.com/category/mathematica/'>Mathematica</a>, <a href='http://coldlogics.wordpress.com/category/sql-server/'>SQL Server</a> Tagged: <a href='http://coldlogics.wordpress.com/tag/geospatial/'>Geospatial</a>, <a href='http://coldlogics.wordpress.com/tag/map/'>Map</a>, <a href='http://coldlogics.wordpress.com/tag/mathematica/'>Mathematica</a>, <a href='http://coldlogics.wordpress.com/tag/microsoft-sql-server/'>Microsoft SQL Server</a>, <a href='http://coldlogics.wordpress.com/tag/sql-server/'>SQL Server</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coldlogics.wordpress.com/350/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coldlogics.wordpress.com/350/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coldlogics.wordpress.com&#038;blog=17292051&#038;post=350&#038;subd=coldlogics&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://coldlogics.wordpress.com/2012/02/21/plotting-latitudelongitude-points-in-mathematica-using-the-sql-server-2008-geography-data-type/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0fcb3c49aad00374ad5d43b9e6a551cd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">phouseak</media:title>
		</media:content>

		<media:content url="http://coldlogics.files.wordpress.com/2012/02/022112_1633_plottinglat1.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/02/022112_1633_plottinglat2.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/02/022112_1633_plottinglat3.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/02/022112_1633_plottinglat4.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2012/02/022112_1633_plottinglat5.png" medium="image" />
	</item>
		<item>
		<title>Connecting Mathematica to SQL Azure Using Microsoft&#8217;s JDBC Drivers</title>
		<link>http://coldlogics.wordpress.com/2011/12/21/connecting-mathematica-to-sql-azure-using-microsofts-jdbc-drivers/</link>
		<comments>http://coldlogics.wordpress.com/2011/12/21/connecting-mathematica-to-sql-azure-using-microsofts-jdbc-drivers/#comments</comments>
		<pubDate>Wed, 21 Dec 2011 18:15:23 +0000</pubDate>
		<dc:creator>phouseak</dc:creator>
				<category><![CDATA[Mathematica]]></category>
		<category><![CDATA[SQL Azure]]></category>
		<category><![CDATA[ClassPath]]></category>
		<category><![CDATA[Java Database Connectivity]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Microsoft SQL Server]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://coldlogics.wordpress.com/?p=333</guid>
		<description><![CDATA[This post will walk you through establishing a database connection in Mathematica to SQL Azure using Microsoft&#8217;s SQL Server JDBC driver. SQL Azure is Microsoft&#8217;s cloud based database service. I&#8217;ve personally enjoyed using SQL Azure for working with data where latency isn&#8217;t a factor. A personal example of where it&#8217;s made my life easier is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coldlogics.wordpress.com&#038;blog=17292051&#038;post=333&#038;subd=coldlogics&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This post will walk you through establishing a <a class="zem_slink" title="Database connection" href="http://en.wikipedia.org/wiki/Database_connection" rel="wikipedia">database connection</a> in <a class="zem_slink" title="Mathematica" href="http://www.wolfram.com/products/mathematica/" rel="homepage">Mathematica</a> to <a class="zem_slink" title="SQL" href="http://www.iso.org/iso/catalogue_detail.htm?csnumber=45498" rel="homepage">SQL</a> Azure using <a href="http://msdn.microsoft.com/en-us/sqlserver/aa937724">Microsoft&#8217;s SQL Server JDBC driver</a>. <a href="http://www.windowsazure.com/en-us/home/tour/database-management/">SQL Azure</a> is Microsoft&#8217;s cloud based database service. I&#8217;ve personally enjoyed using SQL Azure for working with data where latency isn&#8217;t a factor. A personal example of where it&#8217;s made my life easier is that I&#8217;ve used SQL Azure in the classroom to expedite the grading process, dropping the amount of time from days to hours between work done by the students and their reflected grade. I&#8217;ve received a lot of compliments from my students for being able to do so.</p>
<p>This walk through connects to the SQL Azure AdventureWorks database which you can download <a href="http://msftdbprodsamples.codeplex.com/releases/view/37304">here</a>.</p>
<p>The first thing to do is download the <a class="zem_slink" title="Java Database Connectivity" href="http://download.oracle.com/javase/6/docs/technotes/guides/jdbc" rel="homepage">JDBC</a> driver and unpack it to C:\Program Files\<a class="zem_slink" title="Microsoft SQL Server" href="http://www.microsoft.com/sqlserver" rel="homepage">Microsoft SQL Server</a> JDBC Driver 3.0\.</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/12/122111_1815_connectingm1.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span>Once the driver is unpacked, I navigated to sqljdbc_3.0 and removed the sqljdbc.jar file and left sqljdbc4.jar. When I was working with Wolfram support (who, by the way, are extremely knowledgable and professional), we had some issues with <a class="zem_slink" title="Classpath (Java)" href="http://en.wikipedia.org/wiki/Classpath_%28Java%29" rel="wikipedia">ClassPath</a> and the two drivers. The way we resolved this is by removing that file.</p>
<p>Once you&#8217;re just left with sqljdbc4.jar, open up Mathematica and create a new notebook file. The notebook will require both Jlink and DatabaseLink so use the <a href="http://reference.wolfram.com/mathematica/ref/Needs.html">Needs[]</a> command to import both of those.</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/12/122111_1815_connectingm2.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span>Next, we&#8217;ll need to add the Microsoft JDBC driver to the class path so execute:</p>
<p style="margin-left:27pt;"><span style="color:#595959;">AddToClassPath["C:\\Program Files\\Microsoft SQL Server JDBC Driver\\sqljdbc_3.0\\enu\\sqljdbc4.jar"];<br />
</span></p>
<p>Like so:</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/12/122111_1815_connectingm3.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span>Then execute:</p>
<p style="margin-left:27pt;"><span style="color:#595959;">LoadJavaClass["com.microsoft.sqlserver.jdbc.SQLServerDriver"];</span></p>
<p style="margin-left:27pt;">Like so:</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/12/122111_1815_connectingm4.png?w=565" alt="" /></p>
<p>Now it&#8217;s time to connect to SQL Azure with Mathematica. The command and parameters you&#8217;ll use look like the following:</p>
<p style="margin-left:27pt;"><span style="color:#595959;">queryazure = conn=OpenSQLConnection[JDBC["com.microsoft.sqlserver.jdbc.SQLServerDriver","jdbc:sqlserver://&lt;SERVER&gt;.database.windows.net;databaseName=AdventureWorksLTAZ2008R2;user=&lt;USERNAME&gt;@&lt;SERVER&gt;;password=&lt;PASSWORD&gt;;"],&#8221;Username&#8221;-&gt;&#8221;&lt;USERNAME&gt;@&lt;SERVER&gt;&#8221;,&#8221;Password&#8221;-&gt;&#8221;&lt;PASSWORD&#8221;]</span></p>
<p>My connection appears as the following (I&#8217;ve blotted out the sensitive information):</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/12/122111_1815_connectingm5.png?w=565" alt="" /></p>
<p>Once the connection is made, you can confirm your connection by executing</p>
<p style="margin-left:27pt;"><span style="color:#595959;">SQLTableNames[queryazure]</span></p>
<p><img src="http://coldlogics.files.wordpress.com/2011/12/122111_1815_connectingm6.png?w=565" alt="" /></p>
<p>Finally, let&#8217;s query for something specifically. Below is a screenshot looking at the SalesLT.Product table via the web interface:</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/12/122111_1815_connectingm7.png?w=565" alt="" /></p>
<p>One may can also connect with SQL Server Management Studio 2008 R2 as well:</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/12/122111_1815_connectingm8.png?w=565" alt="" /></p>
<p>The query we will want to execute is a simple one: &#8220;<a class="zem_slink" title="Select (SQL)" href="http://en.wikipedia.org/wiki/Select_%28SQL%29" rel="wikipedia">SELECT</a> * FROM SalesLT.Product&#8221;. To do so, we just execute the following:</p>
<p style="margin-left:27pt;"><span style="color:#595959;">query =SQLExecute[queryazure, "SELECT *<br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#595959;">FROM SalesLT.Product"]</span></p>
<p>The output is as follows:</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/12/122111_1815_connectingm9.png?w=565" alt="" /></p>
<p>That&#8217;s it! Enjoy playing with Mathematica and SQL Azure! There&#8217;s lots of blog posts on different things to do and they can be applied to SQL Azure just the same. Some of my favorites are: <a href="http://coldlogics.wordpress.com/2011/05/15/graphing-a-hierarchy-from-a-common-table-expression-using-treeplot/">Graphing a Hierarchy from a Common Table Expression Using TreePlot</a>, <a href="http://coldlogics.wordpress.com/2011/06/12/graphing-foreign-keys-in-sql-server-using-mathematica/">Graphing Foreign Keys in SQL Server Using Mathematica</a>, and <a href="http://coldlogics.wordpress.com/2011/07/23/foreign-keys-as-an-incidence-matrix-a-high-level-look/">Foreign Keys as an Incidence Matrix: A High Level Look</a>.</p>
<p>Finally, I want to credit Wolfram support for their assistance in getting me to connect. They worked with me in fixing my erronious JDBC configuration in Mathematica. Without them, this post wouldn&#8217;t be possible. Thank you Wolfram support!</p>
<br />Filed under: <a href='http://coldlogics.wordpress.com/category/mathematica/'>Mathematica</a>, <a href='http://coldlogics.wordpress.com/category/sql-azure/'>SQL Azure</a> Tagged: <a href='http://coldlogics.wordpress.com/tag/classpath/'>ClassPath</a>, <a href='http://coldlogics.wordpress.com/tag/java-database-connectivity/'>Java Database Connectivity</a>, <a href='http://coldlogics.wordpress.com/tag/jdbc/'>JDBC</a>, <a href='http://coldlogics.wordpress.com/tag/mathematica/'>Mathematica</a>, <a href='http://coldlogics.wordpress.com/tag/microsoft/'>Microsoft</a>, <a href='http://coldlogics.wordpress.com/tag/microsoft-sql-server/'>Microsoft SQL Server</a>, <a href='http://coldlogics.wordpress.com/tag/sql/'>SQL</a>, <a href='http://coldlogics.wordpress.com/tag/sql-azure/'>SQL Azure</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coldlogics.wordpress.com/333/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coldlogics.wordpress.com/333/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coldlogics.wordpress.com&#038;blog=17292051&#038;post=333&#038;subd=coldlogics&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://coldlogics.wordpress.com/2011/12/21/connecting-mathematica-to-sql-azure-using-microsofts-jdbc-drivers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0fcb3c49aad00374ad5d43b9e6a551cd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">phouseak</media:title>
		</media:content>

		<media:content url="http://coldlogics.files.wordpress.com/2011/12/122111_1815_connectingm1.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/12/122111_1815_connectingm2.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/12/122111_1815_connectingm3.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/12/122111_1815_connectingm4.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/12/122111_1815_connectingm5.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/12/122111_1815_connectingm6.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/12/122111_1815_connectingm7.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/12/122111_1815_connectingm8.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/12/122111_1815_connectingm9.png" medium="image" />
	</item>
		<item>
		<title>The AdventureWorks Database as a Transversal Matroid: Part I – Generating a Bipartite Graph</title>
		<link>http://coldlogics.wordpress.com/2011/09/18/the-adventureworks-database-as-a-transversal-matroid-part-i-%e2%80%93-generating-a-bipartite-graph/</link>
		<comments>http://coldlogics.wordpress.com/2011/09/18/the-adventureworks-database-as-a-transversal-matroid-part-i-%e2%80%93-generating-a-bipartite-graph/#comments</comments>
		<pubDate>Mon, 19 Sep 2011 04:43:05 +0000</pubDate>
		<dc:creator>phouseak</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Mathematica]]></category>
		<category><![CDATA[AdventureWorks]]></category>
		<category><![CDATA[bipartite]]></category>
		<category><![CDATA[Graph]]></category>
		<category><![CDATA[Matroid]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://coldlogics.wordpress.com/2011/09/18/the-adventureworks-database-as-a-transversal-matroid-part-i-%e2%80%93-generating-a-bipartite-graph/</guid>
		<description><![CDATA[In previous posts, we took the foreign keys present in the AdventureWorks2008R2 database and used them to represent the database as a multigraph, a simple graph, and an incidence matrix. We even calculated properties such as graph radius and periphery. Another way to investigate relationships between objects is to present them as a matroid (or combinatorial [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coldlogics.wordpress.com&#038;blog=17292051&#038;post=300&#038;subd=coldlogics&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In <a href="http://coldlogics.wordpress.com/2011/06/24/using-mathematica-to-understand-foreign-key-relationships-in-a-database/">previous</a> <a href="http://coldlogics.wordpress.com/2011/07/23/foreign-keys-as-an-incidence-matrix-a-high-level-look/">posts</a>, we took the foreign keys present in the AdventureWorks2008R2 <a class="zem_slink" title="Database" href="http://en.wikipedia.org/wiki/Database" rel="wikipedia">database</a> and used them to represent the database as a <a href="http://mathworld.wolfram.com/Multigraph.html">multigraph</a>, a <a href="http://mathworld.wolfram.com/SimpleGraph.html">simple graph</a>, and an <a href="http://mathworld.wolfram.com/IncidenceMatrix.html">incidence matrix</a>. We even calculated properties such as <a href="http://mathworld.wolfram.com/GraphRadius.html">graph radius</a> and <a href="http://mathworld.wolfram.com/GraphPeriphery.html">periphery</a>. Another way to investigate relationships between objects is to present them as a <a href="http://en.wikipedia.org/wiki/Matroid">matroid</a> (or <a href="http://mathworld.wolfram.com/Matroid.html">combinatorial pre-geometry</a>).</p>
<p>To start, the simple graph of the finite key relationships present in the <a class="zem_slink" title="Contoso" href="http://en.wikipedia.org/wiki/Contoso" rel="wikipedia">AdventureWorks</a> database is already a cycle matroid. This is a very well known result as all finite graphs are cycle matroids. The independent sets in a <a href="http://en.wikipedia.org/wiki/Matroid">cycle matroid</a> are the edges that do not form a <a href="http://planetmath.org/encyclopedia/OpenWalk.html">closed path</a>.  Arguably, the cycle matroid derived from the AdventureWorks database graph may have some interesting properties.</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/09/091911_0442_theadventur1.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Instead of investigating the properties of this cycle matroid, we will investigate the AdventureWorks database as a transversal matroid. The definition of a transversal matroid in <a href="http://www.goodbooksbadcoffee.com/book/9780199603398">&#8220;Matroid Theory&#8221; by James Oxley</a> is as follows:<img src="http://coldlogics.files.wordpress.com/2011/09/091911_0442_theadventur2.png?w=644&#038;h=118" alt="" width="644" height="118" /></p>
<p>A way to visualize transversals is via <a href="http://mathworld.wolfram.com/BipartiteGraph.html">bipartite graphs</a>:</p>
<p style="margin-left:27pt;"><img src="http://coldlogics.files.wordpress.com/2011/09/091911_0442_theadventur3.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>The AdventureWorks database actually works as a great candidate for a transversal matroid. The family of subsets of the AdventureWorks database is the schemas. The AdventureWorks2008R2 database has the following <a href="http://msdn.microsoft.com/en-us/library/dd207005.aspx">schemas</a>: dbo, HumanResources, Person, Production, Purchasing, and Sales. It&#8217;s easy to query the database for its schemas:</p>
<p style="margin-left:27pt;"><span style="color:#595959;">SELECT DISTINCT SCHEMA_NAME(schema_id) FROM sys.tables</span></p>
<p style="margin-left:27pt;">The expected output is shown below.</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/09/091911_0442_theadventur4.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span>To visualize the relationship between tables and schemas, we will need to create a bipartite graph of the relationship. We will create a <a href="http://coldlogics.wordpress.com/2010/12/11/connecting-mathematica-to-sql-server/">connection to the database</a> the usual way. Once that is accomplished, we will need to run the following command:</p>
<p style="margin-left:27pt;"><img src="http://coldlogics.files.wordpress.com/2011/09/091911_0442_theadventur5.png?w=565" alt="" /><span style="color:#595959;font-family:Cambria Math;"><br />
</span></p>
<p>The reason we use <a href="http://msdn.microsoft.com/en-us/library/ms181765.aspx">CASE</a> in the T-<a class="zem_slink" title="SQL" href="http://www.iso.org/iso/catalogue_detail.htm?csnumber=45498" rel="homepage">SQL</a> statement is because there&#8217;s a table named Person too. So to take care of the fact that there&#8217;s a schema and a table of the same name, we just rename it. An partial listing of the output is show below:</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/09/091911_0442_theadventur6.png?w=565" alt="" /></p>
<p>We will need to combine the two columns into something <a class="zem_slink" title="Mathematica" href="http://www.wolfram.com/products/mathematica/" rel="homepage">Mathematica</a> will like to work with for graphing.</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/09/091911_0442_theadventur7.png?w=565" alt="" /></p>
<p>I tried having the <a class="zem_slink" title="Select (SQL)" href="http://en.wikipedia.org/wiki/Select_%28SQL%29" rel="wikipedia">SQL query</a> seamlessly put out columns of the form &#8220;SpecialOffer&#8221; -&gt; &#8220;Sales&#8221;. While the query executed the way one would expect in <a class="zem_slink" title="SQL Server Management Studio" href="http://en.wikipedia.org/wiki/SQL_Server_Management_Studio" rel="wikipedia">SSMS</a>, the parser Mathematica uses choked on it. So here we are, inelegantly combining the results. It is what it is. Perhaps I&#8217;ll find the trick.</p>
<p>We apply the custom function Combine to the query result set.</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/09/091911_0442_theadventur8.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span>Finally, it&#8217;s time to see something worth looking at. The command below:</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/09/091911_0442_theadventur9.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span>Returns:</p>
<p><a href="http://coldlogics.files.wordpress.com/2011/09/adventureworksbipartite.png"><img src="http://coldlogics.files.wordpress.com/2011/09/091911_0442_theadventur10.png?w=565" alt="" /></a><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span>To see the entire graph, click on the image. It&#8217;s very large.</p>
<p>While we&#8217;ve generated a great looking image of the relationship between schemas and tables in the AdventureWorks database, we have not yet described the matroid yet. In the second part of this series, we will generate the maximal independent sets to describe this matroid. I&#8217;m looking forward to it!</p>
<br />Filed under: <a href='http://coldlogics.wordpress.com/category/database/'>Database</a>, <a href='http://coldlogics.wordpress.com/category/mathematica/'>Mathematica</a> Tagged: <a href='http://coldlogics.wordpress.com/tag/adventureworks/'>AdventureWorks</a>, <a href='http://coldlogics.wordpress.com/tag/bipartite/'>bipartite</a>, <a href='http://coldlogics.wordpress.com/tag/graph/'>Graph</a>, <a href='http://coldlogics.wordpress.com/tag/mathematica/'>Mathematica</a>, <a href='http://coldlogics.wordpress.com/tag/matroid/'>Matroid</a>, <a href='http://coldlogics.wordpress.com/tag/sql/'>SQL</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coldlogics.wordpress.com/300/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coldlogics.wordpress.com/300/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coldlogics.wordpress.com&#038;blog=17292051&#038;post=300&#038;subd=coldlogics&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://coldlogics.wordpress.com/2011/09/18/the-adventureworks-database-as-a-transversal-matroid-part-i-%e2%80%93-generating-a-bipartite-graph/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0fcb3c49aad00374ad5d43b9e6a551cd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">phouseak</media:title>
		</media:content>

		<media:content url="http://coldlogics.files.wordpress.com/2011/09/091911_0442_theadventur1.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/09/091911_0442_theadventur2.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/09/091911_0442_theadventur3.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/09/091911_0442_theadventur4.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/09/091911_0442_theadventur5.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/09/091911_0442_theadventur6.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/09/091911_0442_theadventur7.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/09/091911_0442_theadventur8.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/09/091911_0442_theadventur9.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/09/091911_0442_theadventur10.png" medium="image" />
	</item>
		<item>
		<title>Foreign Keys as an Incidence Matrix: A High Level Look</title>
		<link>http://coldlogics.wordpress.com/2011/07/23/foreign-keys-as-an-incidence-matrix-a-high-level-look/</link>
		<comments>http://coldlogics.wordpress.com/2011/07/23/foreign-keys-as-an-incidence-matrix-a-high-level-look/#comments</comments>
		<pubDate>Sat, 23 Jul 2011 21:44:32 +0000</pubDate>
		<dc:creator>phouseak</dc:creator>
				<category><![CDATA[Mathematica]]></category>
		<category><![CDATA[AdventureWorks]]></category>
		<category><![CDATA[Column]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Foreign key]]></category>
		<category><![CDATA[Graph]]></category>
		<category><![CDATA[Incidence Matrix]]></category>
		<category><![CDATA[Matrix]]></category>
		<category><![CDATA[Microsoft SQL Server]]></category>

		<guid isPermaLink="false">http://coldlogics.wordpress.com/2011/07/23/foreign-keys-as-an-incidence-matrix-a-high-level-look/</guid>
		<description><![CDATA[In a previous post, we took a multigraph of foreign keys in the AdventureWorks database in SQL Server 2008 and manipulated it into a simple graph. This yielded a high level view of the foreign keys in a database. A perfect supplementary diagram to any database diagram. We were also able to derive some interesting [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coldlogics.wordpress.com&#038;blog=17292051&#038;post=285&#038;subd=coldlogics&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In a <a href="http://coldlogics.wordpress.com/2011/06/24/using-mathematica-to-understand-foreign-key-relationships-in-a-database/">previous post</a>, we took a <a href="http://mathworld.wolfram.com/Multigraph.html">multigraph</a> of <a href="http://msdn.microsoft.com/en-us/library/ms177463.aspx">foreign keys</a> in the <a href="http://msftdbprodsamples.codeplex.com/">AdventureWorks</a> <a class="zem_slink" title="Database" href="http://en.wikipedia.org/wiki/Database" rel="wikipedia">database</a> in <a href="http://www.microsoft.com/sqlserver/en/us/default.aspx">SQL Server 2008</a> and manipulated it into a <a href="http://mathworld.wolfram.com/SimpleGraph.html">simple graph</a>. This yielded a high level view of the foreign keys in a database. A perfect supplementary diagram to any <a href="http://en.wikipedia.org/wiki/Database_model">database diagram</a>. We were also able to derive some interesting and potentially useful facts regarding foreign keys in the database such as the <a href="http://mathworld.wolfram.com/GraphCenter.html">graph center</a> and <a href="http://mathworld.wolfram.com/GraphPeriphery.html">graph periphery</a>.</p>
<p>The graph from our previous post is below:</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/07/072311_2144_foreignkeys1.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>It really is a pretty constellation of vertices and edges. It&#8217;s possible to go a few steps further and manipulate this captured information in other ways. For example, one could create an <a href="http://mathworld.wolfram.com/IncidenceMatrix.html">incidence matrix</a> of the graph. An incidence matrix where the <a class="zem_slink" title="Column" href="http://en.wikipedia.org/wiki/Column" rel="wikipedia">columns</a> are the edges and the rows are the vertices of a graph. If a vertex i is connected to an edge j, then the i,j entry of the incidence matrix is 1, otherwise, it&#8217;s marked 0.</p>
<p>In our previous example, we created a graph named fkgraph by executing:</p>
<p style="margin-left:27pt;"><span style="color:#7f7f7f;">fkgraph = Graph[querygraph]<br />
</span></p>
<p><img src="http://coldlogics.files.wordpress.com/2011/07/072311_2144_foreignkeys2.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>To retrieve an incidence matrix from this graph, we may use the <a href="http://reference.wolfram.com/mathematica/ref/IncidenceMatrix.html">IncidenceMatrix</a> function in <a class="zem_slink" title="Mathematica" href="http://www.wolfram.com/products/mathematica/" rel="homepage">Mathematica</a>. Once executed, we may execute:</p>
<p style="margin-left:27pt;"><span style="color:#7f7f7f;">IncidenceMatrix[fkgraph]//MatrixForm.</span></p>
<p>If we do not utilize the <a href="http://reference.wolfram.com/mathematica/ref/Postfix.html">PostFix</a> function (//) to format IncidenceMatrix function with the function <a href="http://reference.wolfram.com/mathematica/ref/MatrixForm.html">MatrixForm</a>, we will get a sparse array like below.</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/07/072311_2144_foreignkeys3.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>A partial output of the desired output is captured below:</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/07/072311_2144_foreignkeys4.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>If we look above at the <a href="http://reference.wolfram.com/mathematica/ref/SparseArray.html">SparseArray</a> output, we can immediately see that the number of rows of the incidence matrix is sixty-seven. That is, there are sixty-seven points in the graph. Consequentially, we know that there are sixty-seven tables with foreign key relationships in the AdventureWorks database as each point represents a table. There are simpler ways of deriving that information but as one proceeds to more abstract layers, it&#8217;s good to remember what one is actually working with.</p>
<p>What about the matrix itself? What is the <a href="http://mathworld.wolfram.com/MatrixRank.html">rank</a> of the matrix? To understand the rank of this matrix, we will use the Mathematica function <a href="http://reference.wolfram.com/mathematica/ref/MatrixRank.html">MatrixRank</a>.</p>
<p style="margin-left:27pt;"><span style="color:#7f7f7f;">MatrixRank[IncidenceMatrix[fkgraph]]<span style="font-family:Cambria Math;"><br />
</span></span></p>
<p>The output is:</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/07/072311_2144_foreignkeys5.png?w=565" alt="" /></p>
<p>Sixty-seven. Who knew? It&#8217;s a <a href="http://en.wikipedia.org/wiki/Rank_(linear_algebra)">well known fact</a> that the <a class="zem_slink" title="Rank (linear algebra)" href="http://en.wikipedia.org/wiki/Rank_%28linear_algebra%29" rel="wikipedia">column rank</a> and the row rank of a matrix are equal. Sixty-seven is the number of rows (points) in the matrix and eighty-six is the number of columns (edges). This would imply that the difference between total columns and the size of the column space is the number of dependent columns. Dependent columns are, in this representation, different choices one could make to JOIN one table with another.</p>
<p>The matrix itself is rather overwhelming. To get a better at a glance view of what the matrix looks like, we can use the <a href="http://reference.wolfram.com/mathematica/ref/MatrixPlot.html">MatrixPlot</a> function.</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/07/072311_2144_foreignkeys6.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>The orange squares represent the value of one and the white of zero. In this sense, we have a rough understanding of the dispersion of ones and zeroes throughout the matrix.</p>
<p>I hope I&#8217;ve introduced enough tools in Mathematica to get one started with manipulating datasets and beginning to derive a deeper understanding regarding their properties. The topic is very deep and there&#8217;s much to learn and discover.</p>
<br />Filed under: <a href='http://coldlogics.wordpress.com/category/mathematica/'>Mathematica</a> Tagged: <a href='http://coldlogics.wordpress.com/tag/adventureworks/'>AdventureWorks</a>, <a href='http://coldlogics.wordpress.com/tag/column/'>Column</a>, <a href='http://coldlogics.wordpress.com/tag/database/'>Database</a>, <a href='http://coldlogics.wordpress.com/tag/foreign-key/'>Foreign key</a>, <a href='http://coldlogics.wordpress.com/tag/graph/'>Graph</a>, <a href='http://coldlogics.wordpress.com/tag/incidence-matrix/'>Incidence Matrix</a>, <a href='http://coldlogics.wordpress.com/tag/mathematica/'>Mathematica</a>, <a href='http://coldlogics.wordpress.com/tag/matrix/'>Matrix</a>, <a href='http://coldlogics.wordpress.com/tag/microsoft-sql-server/'>Microsoft SQL Server</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coldlogics.wordpress.com/285/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coldlogics.wordpress.com/285/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coldlogics.wordpress.com&#038;blog=17292051&#038;post=285&#038;subd=coldlogics&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://coldlogics.wordpress.com/2011/07/23/foreign-keys-as-an-incidence-matrix-a-high-level-look/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0fcb3c49aad00374ad5d43b9e6a551cd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">phouseak</media:title>
		</media:content>

		<media:content url="http://coldlogics.files.wordpress.com/2011/07/072311_2144_foreignkeys1.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/07/072311_2144_foreignkeys2.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/07/072311_2144_foreignkeys3.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/07/072311_2144_foreignkeys4.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/07/072311_2144_foreignkeys5.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/07/072311_2144_foreignkeys6.png" medium="image" />
	</item>
		<item>
		<title>Using Mathematica to Understand Foreign Key Relationships in a Database</title>
		<link>http://coldlogics.wordpress.com/2011/06/24/using-mathematica-to-understand-foreign-key-relationships-in-a-database/</link>
		<comments>http://coldlogics.wordpress.com/2011/06/24/using-mathematica-to-understand-foreign-key-relationships-in-a-database/#comments</comments>
		<pubDate>Sat, 25 Jun 2011 08:26:20 +0000</pubDate>
		<dc:creator>phouseak</dc:creator>
				<category><![CDATA[Mathematica]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Distance (graph theory)]]></category>
		<category><![CDATA[Foreign key]]></category>
		<category><![CDATA[Join (SQL)]]></category>
		<category><![CDATA[Microsoft SQL Server]]></category>

		<guid isPermaLink="false">http://coldlogics.wordpress.com/2011/06/24/using-mathematica-to-understand-foreign-key-relationships-in-a-database/</guid>
		<description><![CDATA[In a previous post, we extracted information about foreign keys in the AdventureWorks database from SQL Server and plotted them in a multigraph in Mathematica. This gives us a high level perspective of the relationships between all the tables in a database. A lot of information is abstracted out but those details are easily found [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coldlogics.wordpress.com&#038;blog=17292051&#038;post=271&#038;subd=coldlogics&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In a <a href="http://coldlogics.wordpress.com/2011/06/12/graphing-foreign-keys-in-sql-server-using-mathematica/">previous post</a>, we extracted information about <a class="zem_slink" title="Foreign key" href="http://en.wikipedia.org/wiki/Foreign_key" rel="wikipedia">foreign keys</a> in the <a class="zem_slink" title="Contoso" href="http://en.wikipedia.org/wiki/Contoso" rel="wikipedia">AdventureWorks</a> <a class="zem_slink" title="Database" href="http://en.wikipedia.org/wiki/Database" rel="wikipedia">database</a> from <a class="zem_slink" title="Microsoft SQL Server" href="http://www.microsoft.com/sqlserver" rel="homepage">SQL Server</a> and plotted them in a <a href="http://mathworld.wolfram.com/Multigraph.html">multigraph</a> in <a class="zem_slink" title="Mathematica" href="http://www.wolfram.com/products/mathematica/" rel="homepage">Mathematica</a>. This gives us a high level perspective of the relationships between all the tables in a database. A lot of information is abstracted out but those details are easily found by including certain parameters in the Mathematica commands we&#8217;ve run or by using the usual database diagramming tools (<a class="zem_slink" title="SQL Server Management Studio" href="http://en.wikipedia.org/wiki/SQL_Server_Management_Studio" rel="wikipedia">SSMS</a> or <a class="zem_slink" title="Microsoft Visio" href="http://office.microsoft.com/en-us/visio/" rel="homepage">Visio</a> for example).</p>
<p>Picking up where we left off, we just created a multigraph of the <a href="http://msdn.microsoft.com/en-us/library/ms175464.aspx">foreign keys</a> in AdventureWorks that looked like this:</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/06/062511_0825_usingmathem1.png?w=565" alt="" /></p>
<p>The next thing that we&#8217;d like to be able to do is take this graph and make it something that we can perform further operations on to gain a deeper understanding of our database structure. To start, we need Mathematica to be able to create a graph object out of the result set. This means the graph must no longer have parallel lines. It must be a <a href="http://mathworld.wolfram.com/SimpleGraph.html">simple graph</a>. The simple way to do this, especially since we no longer need the foreign key names is to massage the query a little bit:</p>
<p style="margin-left:27pt;"><span style="color:#7f7f7f;">SELECT DISTINCT OBJECT_NAME(f.parent_object_id) AS TableName,<br />
</span></p>
<p style="margin-left:54pt;"><span style="color:#7f7f7f;">OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName<br />
</span></p>
<p style="margin-left:54pt;"><span style="color:#7f7f7f;">FROM sys.foreign_keys AS f<br />
</span></p>
<p style="margin-left:54pt;"><span style="color:#7f7f7f;"><a class="zem_slink" title="Join (SQL)" href="http://en.wikipedia.org/wiki/Join_%28SQL%29" rel="wikipedia">INNER JOIN</a> sys.foreign_key_columns AS fc<br />
</span></p>
<p style="margin-left:54pt;"><span style="color:#7f7f7f;">ON f.OBJECT_ID = fc.constraint_object_id<br />
</span></p>
<p>By doing this, all the duplicate rows will be removed.</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/06/062511_0825_usingmathem2.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>A quick scan and comparison of the two queries will confirm that the total number of repeated tables in the first query was five and that the duplicates have been removed. I would love, love, love to craft a query that returns distinct pairs of values along with columns with duplicate information but I don&#8217;t have the requisite skill to craft that query in an acceptable amount of time. I intend to fix that. It&#8217;s an interesting problem. Now that the duplicate values have been removed, we may perform the typical operations that we used before. To begin, we open up the notebook we created before or input and execute the commands as shown below:</p>
<p><a href="http://coldlogics.files.wordpress.com/2011/06/foreignkeyconnandquery.png"><img src="http://coldlogics.files.wordpress.com/2011/06/062511_0825_usingmathem3.png?w=565" alt="" /></a><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Once completed, the next step is to create the substitution <a href="http://reference.wolfram.com/mathematica/ref/Function.html">function</a> in Mathematica. To make the <a href="http://reference.wolfram.com/mathematica/ref/UndirectedEdge.html">undirected edge</a> symbol in Mathematica, hit the ESC key, type, &#8220;ue&#8221; and hit ESC again. &lt;-&gt; suffices as well if you like ugly.</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/06/062511_0825_usingmathem4.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>And, of course, apply to the result set from SQL Server:</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/06/062511_0825_usingmathem5.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>The set up is complete, next we will just apply <a href="http://reference.wolfram.com/mathematica/ref/Graph.html">Graph[]</a> to the modified list:</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/06/062511_0825_usingmathem6.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>The first question we may get the answer to now that the foreign key relationships in AdventureWorks are represented in a <a href="http://mathworld.wolfram.com/SimpleGraph.html">simple graph</a> is what is the maximum minimum number of joins one must perform to join any one table in the database with another? What about the minimum maximum? To do this, we will want to calculate the <a href="http://mathworld.wolfram.com/GraphDiameter.html">graph diameter</a> and <a href="http://mathworld.wolfram.com/GraphRadius.html">graph radius</a> of the graph, respectively. The answer turns out to be nine and five. Who knew?</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/06/062511_0825_usingmathem7.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>What about knowing which tables have the most or least amount of <a href="http://mathworld.wolfram.com/GraphEccentricity.html">graph eccentricity</a>? Very easy to answer:</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/06/062511_0825_usingmathem8.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>The central most tables in AdventureWorks are Document, PurchaseOrderHeader, and SalesOrderDetail while the most isolated tables are CountryRegionCurrency, PhoneNumberType, Illustration, Culture, ProductDescription, and SalesTaxRate. It certainly beats trying to figure it by looking at the diagram snippet below and trying to figure it out.</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/06/062511_0825_usingmathem9.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>What&#8217;s the real world consequence of this? Using Mathematica in this respect can reduce the amount of time it takes to understand the high level structure of your database. Specifically, some poorly running queries may be associated with extensive use of joins. In his paper, &#8220;<a href="http://infolab.stanford.edu/~widom/cs346/ioannidis.pdf">Query Optimization</a>&#8220;, Yannis Ioannidis asserts,</p>
<p style="margin-left:27pt;">&#8220;<em>The memory requirements and running time of dynamic programming grow exponentially with query size (i.e., number of joins) in the worst case since all viable partial plans generated in each step must be stored to be used in the next one. In fact, many modern systems place a limit on the size of queries that can be submitted (usually around fifteen joins), because for larger queries the optimizer crashes due to its very high memory requirements. Nevertheless, most queries seen in practice involve less than ten joins, and the algorithm has proved to be very effective in such contexts</em>&#8221; (referring to dynamic programming algorithms).</p>
<p>It&#8217;s worth noting that the paper was written in 1996 so many things have changed but the fundamental issue of joins being costly will remain true, especially as we continue to experience the explosive data growth that we have. Performing these analytical steps is no silver bullet. Much of performance tuning is correlating information from a variety of sources to understand what database users are looking for and how SQL Server is trying to respond to it.</p>
<br />Filed under: <a href='http://coldlogics.wordpress.com/category/mathematica/'>Mathematica</a>, <a href='http://coldlogics.wordpress.com/category/sql-server/'>SQL Server</a> Tagged: <a href='http://coldlogics.wordpress.com/tag/database/'>Database</a>, <a href='http://coldlogics.wordpress.com/tag/distance-graph-theory/'>Distance (graph theory)</a>, <a href='http://coldlogics.wordpress.com/tag/foreign-key/'>Foreign key</a>, <a href='http://coldlogics.wordpress.com/tag/join-sql/'>Join (SQL)</a>, <a href='http://coldlogics.wordpress.com/tag/mathematica/'>Mathematica</a>, <a href='http://coldlogics.wordpress.com/tag/microsoft-sql-server/'>Microsoft SQL Server</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coldlogics.wordpress.com/271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coldlogics.wordpress.com/271/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coldlogics.wordpress.com&#038;blog=17292051&#038;post=271&#038;subd=coldlogics&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://coldlogics.wordpress.com/2011/06/24/using-mathematica-to-understand-foreign-key-relationships-in-a-database/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0fcb3c49aad00374ad5d43b9e6a551cd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">phouseak</media:title>
		</media:content>

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/062511_0825_usingmathem1.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/062511_0825_usingmathem2.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/062511_0825_usingmathem3.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/062511_0825_usingmathem4.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/062511_0825_usingmathem5.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/062511_0825_usingmathem6.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/062511_0825_usingmathem7.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/062511_0825_usingmathem8.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/062511_0825_usingmathem9.png" medium="image" />
	</item>
		<item>
		<title>Graphing Foreign Keys in SQL Server Using Mathematica</title>
		<link>http://coldlogics.wordpress.com/2011/06/12/graphing-foreign-keys-in-sql-server-using-mathematica/</link>
		<comments>http://coldlogics.wordpress.com/2011/06/12/graphing-foreign-keys-in-sql-server-using-mathematica/#comments</comments>
		<pubDate>Sun, 12 Jun 2011 18:36:25 +0000</pubDate>
		<dc:creator>phouseak</dc:creator>
				<category><![CDATA[Mathematica]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Foreign key]]></category>
		<category><![CDATA[Graph]]></category>
		<category><![CDATA[Microsoft SQL Server]]></category>

		<guid isPermaLink="false">http://coldlogics.wordpress.com/2011/06/12/graphing-foreign-keys-in-sql-server-using-mathematica/</guid>
		<description><![CDATA[Foreign keys in any relational database form a cornerstone of a well-structured database. One of the challenges with any database diagramming tool is that the output will not be a mathematically manipulatable object. Certain attributes such as graph diameter or graph distance are not readily calculable. If one employs Mathematica, it&#8217;s possible to get a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coldlogics.wordpress.com&#038;blog=17292051&#038;post=254&#038;subd=coldlogics&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/library/ms175464.aspx">Foreign keys</a> in any relational <a class="zem_slink" title="Database" href="http://en.wikipedia.org/wiki/Database" rel="wikipedia">database</a> form a cornerstone of a well-structured database. One of the challenges with any database diagramming tool is that the output will not be a mathematically manipulatable object. Certain attributes such as <a href="http://mathworld.wolfram.com/GraphDiameter.html">graph diameter</a> or <a href="http://mathworld.wolfram.com/GraphDistance.html">graph distance</a> are not readily calculable. If one employs <a class="zem_slink" title="Mathematica" href="http://www.wolfram.com/products/mathematica/" rel="homepage">Mathematica</a>, it&#8217;s possible to get a high level understanding of the <a class="zem_slink" title="Foreign key" href="http://en.wikipedia.org/wiki/Foreign_key" rel="wikipedia">foreign key</a> relationships in a <a class="zem_slink" title="Microsoft SQL Server" href="http://www.microsoft.com/sqlserver" rel="homepage">SQL Server</a> database. Our goal is to take this:</p>
<p style="margin-left:27pt;"><img src="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor1.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>And turn it into something that looks like this:</p>
<p><a href="http://home.wlu.edu/~mcraea/Finite_Geometry/Introduction/Prob5Graphs/Problem5.htm"><img src="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor2.png?w=565" alt="" border="0" /></a><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>The challenge is to extract a <a class="zem_slink" title="Result set" href="http://en.wikipedia.org/wiki/Result_set" rel="wikipedia">result set</a> that will give us what we need. Fortunately for us, great minds can show us the way. Pinal Dave (<a href="http://blog.sqlauthority.com/">SQLAuthority.com</a>) whipped up a <a href="http://blog.sqlauthority.com/2007/09/04/sql-server-2005-find-tables-with-foreign-key-constraint-in-database/">tidy query</a> that forms the basis of what we will use. The query we will use has some slight modifications from the original on Mr. Dave&#8217;s blog:</p>
<p style="margin-left:27pt;"><span style="color:#7f7f7f;"><a class="zem_slink" title="Select (SQL)" href="http://en.wikipedia.org/wiki/Select_%28SQL%29" rel="wikipedia">SELECT</a> OBJECT_NAME(f.parent_object_id) AS TableName<br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#7f7f7f;">,OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName<br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#7f7f7f;">,f.name AS ForeignKey<br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#7f7f7f;">FROM sys.foreign_keys AS f<br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#7f7f7f;"><a class="zem_slink" title="Join (SQL)" href="http://en.wikipedia.org/wiki/Join_%28SQL%29" rel="wikipedia">INNER JOIN</a> sys.foreign_key_columns AS fc<br />
</span></p>
<p style="margin-left:27pt;"><span style="color:#7f7f7f;">ON f.OBJECT_ID = fc.constraint_object_id<br />
</span></p>
<p>The result set for AdventureWorks2008R2 should appear akin to what is below:</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor3.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Next, let&#8217;s get this all into Mathematica. Make sure to set up the notebook file to <a href="http://coldlogics.wordpress.com/2011/01/24/connecting-to-sql-part-ii/">connect to SQL Server</a> like so:</p>
<p><a href="http://coldlogics.files.wordpress.com/2011/02/sqlconnectionmathematica.png"><img src="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor4.png?w=565" alt="" /></a><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Add in the query typed out above:</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor5.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>The output should look like the partial results below:</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor6.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>The Mathematica function we intend to use to visualize the foreign keys in the AdventureWorks2008R2 database is <a href="http://reference.wolfram.com/mathematica/ref/GraphPlot.html">GraphPlot[]</a>. Like <a href="http://reference.wolfram.com/mathematica/ref/TreePlot.html">TreePlot []</a>, a function we used in a <a href="http://coldlogics.wordpress.com/2011/05/15/graphing-a-hierarchy-from-a-common-table-expression-using-treeplot/">similar post</a>, we will need to massage the result set into something Mathematica can use. If we wish to label the edges with the names of the foreign keys then GraphPlot will need to see entries akin to {u -&gt; v, &#8220;w&#8221;}. To do this, we will use the <a href="http://reference.wolfram.com/mathematica/ref/Function.html">Function[]</a> ummmm function.</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor7.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Once that is done, we will need to apply the function to the result set from SQL Server.</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor8.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>At this point, we&#8217;re ready to graph the foreign keys using GraphPlot.</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor9.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>It&#8217;s worth noting that the resulting <a href="http://mathworld.wolfram.com/Multigraph.html">multigraph</a> is very, very busy. Here&#8217;s a screenshot of the graph expanded and a corner zoomed in on.</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor10.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>It&#8217;s possible to vertex label as well but imagine how cluttered the graph will be. Perhaps we want to abstract out the labels and just see the general structure of foreign keys in the database. We merely need to built a new function that does not have edge labeling and use that one instead.</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor11.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>When the function is applied to the result set, we get a flat table of relationships perfect for GraphPlot.</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor12.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Once that is passed to GraphPlot, the resulting graph ensues:</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor13.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>There it is! All the foreign keys in the AdventureWorks2008R2 database seen from a bird&#8217;s eye view. So what next? Are we able to perform any mathematical manipulation on this? We&#8217;ll investigate that in part two. Thanks for reading!</p>
<br />Filed under: <a href='http://coldlogics.wordpress.com/category/mathematica/'>Mathematica</a>, <a href='http://coldlogics.wordpress.com/category/sql-server/'>SQL Server</a> Tagged: <a href='http://coldlogics.wordpress.com/tag/database/'>Database</a>, <a href='http://coldlogics.wordpress.com/tag/foreign-key/'>Foreign key</a>, <a href='http://coldlogics.wordpress.com/tag/graph/'>Graph</a>, <a href='http://coldlogics.wordpress.com/tag/mathematica/'>Mathematica</a>, <a href='http://coldlogics.wordpress.com/tag/microsoft-sql-server/'>Microsoft SQL Server</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coldlogics.wordpress.com/254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coldlogics.wordpress.com/254/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coldlogics.wordpress.com&#038;blog=17292051&#038;post=254&#038;subd=coldlogics&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://coldlogics.wordpress.com/2011/06/12/graphing-foreign-keys-in-sql-server-using-mathematica/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0fcb3c49aad00374ad5d43b9e6a551cd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">phouseak</media:title>
		</media:content>

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor1.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor2.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor3.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor4.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor5.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor6.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor7.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor8.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor9.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor10.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor11.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor12.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/06/061211_1835_graphingfor13.png" medium="image" />
	</item>
		<item>
		<title>Graphing a Hierarchy from a Common Table Expression Using TreePlot</title>
		<link>http://coldlogics.wordpress.com/2011/05/15/graphing-a-hierarchy-from-a-common-table-expression-using-treeplot/</link>
		<comments>http://coldlogics.wordpress.com/2011/05/15/graphing-a-hierarchy-from-a-common-table-expression-using-treeplot/#comments</comments>
		<pubDate>Sun, 15 May 2011 22:44:53 +0000</pubDate>
		<dc:creator>phouseak</dc:creator>
				<category><![CDATA[Mathematica]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Common Table Expression]]></category>
		<category><![CDATA[CTE]]></category>
		<category><![CDATA[Microsoft SQL Server]]></category>
		<category><![CDATA[Select (SQL)]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[TreePlot]]></category>

		<guid isPermaLink="false">http://coldlogics.wordpress.com/2011/05/15/graphing-a-hierarchy-from-a-common-table-expression-using-treeplot/</guid>
		<description><![CDATA[Common table expressions are one of the most underrated tools SQL Server ships with. CTEs greatly simplify querying SQL Server in situations where other queries would have either been extremely difficult or impossible to write. One can learn more about CTEs on MSDN. Further, I&#8217;d highly recommend this write-up by SQL Lion. It&#8217;s very thorough [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coldlogics.wordpress.com&#038;blog=17292051&#038;post=229&#038;subd=coldlogics&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Common_table_expressions">Common table expressions</a> are one of the most underrated tools <a class="zem_slink" title="SQL" href="http://www.iso.org/iso/catalogue_detail.htm?csnumber=45498" rel="homepage">SQL</a> Server ships with. CTEs greatly simplify querying SQL Server in situations where other queries would have either been extremely difficult or impossible to write. One can learn more about <a href="http://msdn.microsoft.com/en-us/library/ms190766.aspx">CTEs on MSDN</a>. Further, I&#8217;d highly recommend this <a href="http://www.sqllion.com/2010/08/common-table-expressions-cte/">write-up</a> by <a href="http://www.sqllion.com/">SQL Lion</a>. It&#8217;s very thorough and provides a detailed breakdown of what is happening in a common table expression.</p>
<p>One of the great powers of CTEs is that they trivialize hierarchical queries. A common example is querying <a class="zem_slink" title="Contoso" href="http://en.wikipedia.org/wiki/Contoso" rel="wikipedia">AdventureWorks</a> to build a list of who reports to whom. Take the following query, shamelessly yanked from SQL Lion (with a tiny modification):</p>
<pre style="margin-left:27pt;">WITH EmpDetails (EmployeeKey, FirstName, LastName, ParentEmployeeKey,</pre>
<pre style="margin-left:27pt;">PFirstName, PLastName, EmployeeRank, [Status])</pre>
<pre style="margin-left:27pt;">AS</pre>
<pre style="margin-left:27pt;">(</pre>
<pre style="margin-left:27pt;"><a class="zem_slink" title="Select (SQL)" href="http://en.wikipedia.org/wiki/Select_%28SQL%29" rel="wikipedia">SELECT</a></pre>
<pre style="margin-left:27pt;">EmployeeKey,FirstName,LastName,ParentEmployeeKey</pre>
<pre style="margin-left:27pt;">,CAST('NULL' AS <a class="zem_slink" title="Varchar" href="http://en.wikipedia.org/wiki/Varchar" rel="wikipedia">VARCHAR</a>(50)) AS PFirstName</pre>
<pre style="margin-left:27pt;">,CAST('NULL' AS VARCHAR(50)) AS PLastName</pre>
<pre style="margin-left:27pt;">,1 AS EmployeeRank,[Status]</pre>
<pre style="margin-left:27pt;">FROM dbo.DimEmployee</pre>
<pre style="margin-left:81pt;">WHERE ParentEmployeeKey IS NULL</pre>
<pre style="margin-left:27pt;">UNION ALL</pre>
<pre style="margin-left:27pt;">SELECT e.EmployeeKey,e.FirstName,e.LastName,e.ParentEmployeeKey</pre>
<pre style="margin-left:81pt;">,CAST(CTE_Emp.FirstName AS VARCHAR(50)) AS PFirstName</pre>
<pre style="margin-left:81pt;">,CAST(CTE_Emp.LastName AS VARCHAR(50)) AS PLastName</pre>
<pre style="margin-left:81pt;">,CTE_Emp.EmployeeRank + 1 AS EmployeeRank</pre>
<pre style="margin-left:81pt;">,e.[Status]</pre>
<pre style="margin-left:27pt;">FROM dbo.DimEmployee e</pre>
<pre style="margin-left:27pt;"><a class="zem_slink" title="Join (SQL)" href="http://en.wikipedia.org/wiki/Join_%28SQL%29" rel="wikipedia">INNER JOIN</a> EmpDetails CTE_Emp</pre>
<pre style="margin-left:27pt;">ON e.ParentEmployeeKey = CTE_Emp.EmployeeKey</pre>
<pre style="margin-left:27pt;">)</pre>
<pre style="margin-left:27pt;">SELECT * FROM EmpDetails WHERE [Status] IS <a class="zem_slink" title="Null (SQL)" href="http://en.wikipedia.org/wiki/Null_%28SQL%29" rel="wikipedia">NOT NULL</a> <a class="zem_slink" title="Order by" href="http://en.wikipedia.org/wiki/Order_by" rel="wikipedia">ORDER BY</a> EmployeeRank</pre>
<p style="margin-left:27pt;">If we query the AdventureWorksDW2008R2 database, we should get a similar result to the <a class="zem_slink" title="Result set" href="http://en.wikipedia.org/wiki/Result_set" rel="wikipedia">result set</a> below:</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/05/051511_2244_graphingahi1.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Reading the result set, we can see that Ken Sánchez reports to no one as his ParentEmployeeKey is NULL. Throughout the rest of the table, we can see we have individuals that have someone else they report to. In tabular format this is difficult to read and even harder to visualize.</p>
<p>Enter <a class="zem_slink" title="Mathematica" href="http://www.wolfram.com/products/mathematica/" rel="homepage">Mathematica</a> and <a href="http://reference.wolfram.com/mathematica/ref/TreePlot.html">TreePlot[]</a>. We want to take the essential data from the above output and create a visual representation. To start, we will want create a <a href="http://:%20http:/coldlogics.wordpress.com/2011/01/24/connecting-to-sql-part-ii/">database connection</a> in our notebook (.nb) file. This is done the usual way with <a href="http://reference.wolfram.com/mathematica/DatabaseLink/tutorial/Introduction.html">DatabaseLink</a>.</p>
<p><a href="http://coldlogics.files.wordpress.com/2011/05/adventureworksdwconnection.png"><img src="http://coldlogics.files.wordpress.com/2011/05/051511_2244_graphingahi2.png?w=565" alt="" /></a><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>Take care to note that for the purposes of this example, we are connecting to the AdventureWorksDW2008R2 database and not AdventureWorks2008R2. Once connected, we will want to use the above query with a small modification.</p>
<p><a href="http://coldlogics.files.wordpress.com/2011/05/ctequerymathematica.png"><img src="http://coldlogics.files.wordpress.com/2011/05/051511_2244_graphingahi3.png?w=565" alt="" /></a><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>We modified the SELECT * from the first query to SELECT EmployeeKey, ParentEmployeeKey. This is due to the fact that the other columns are extraneous to our purpose and will only create problems if we keep them in the result set. Once we run the command, we should receive a result set that appears as follows (the image is cropped):</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/05/051511_2244_graphingahi4.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>If we look at the TreePlot documentation, we note that our list isn&#8217;t quite in a usable state so we will have to massage the data some. The first thing we will do is define a function that will take two parameters <strong>u</strong>, <strong>v</strong>, and return u -&gt; v. To do this, we will use the <a href="http://reference.wolfram.com/mathematica/ref/Function.html">Function[]</a> function in the following manner: Unite = Function[{u,v}, u -&gt; v]</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/05/051511_2244_graphingahi5.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>I&#8217;ve named my function Unite. It&#8217;s a terrible name. Feel free to name yours better. Once the function is built, we need to apply it to the list. To do this, we will use <a href="http://reference.wolfram.com/mathematica/ref/Apply.html">Apply[]</a>.</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/05/051511_2244_graphingahi6.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>We&#8217;ve used shorthand here with the three @ signs. The reason we&#8217;ve done so is that the default behavior of Apply would be to apply the &#8220;Unite&#8221; function at the top level of the list. We want it to apply at one level beneath that. The three @@@ signs is shorthand for doing exactly that. The documentation for Apply discusses it. Also note that the Apply function stripped the sublists out of the list, giving us a litany of relation rules ready for TreePlot. To use TreePlot, merely pass the variable for the relation rules and any options needed. In this example, we want to label our vertices.</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/05/051511_2244_graphingahi7.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>It appears that we have four discrete trees in our hierarchy! While this would seem to be unexpected, a simple verification of the data does show that this is not inaccurate. The trees are quite small and very busy so if we select them, click, and drag, we can see them in better detail.</p>
<p><img src="http://coldlogics.files.wordpress.com/2011/05/051511_2244_graphingahi8.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span><br />
<img src="http://coldlogics.files.wordpress.com/2011/05/051511_2244_graphingahi9.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span><br />
<img src="http://coldlogics.files.wordpress.com/2011/05/051511_2244_graphingahi10.png?w=565" alt="" /><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<p>That&#8217;s it! We&#8217;ve just used a common table expression to query a database for hierarchical relationships and then used Mathematica to graph it. Play around with it and leave any comments you might have. Thanks for reading.</p>
<p><span style="font-family:Times New Roman;font-size:12pt;"><br />
</span></p>
<br />Filed under: <a href='http://coldlogics.wordpress.com/category/mathematica/'>Mathematica</a>, <a href='http://coldlogics.wordpress.com/category/sql-server/'>SQL Server</a> Tagged: <a href='http://coldlogics.wordpress.com/tag/common-table-expression/'>Common Table Expression</a>, <a href='http://coldlogics.wordpress.com/tag/cte/'>CTE</a>, <a href='http://coldlogics.wordpress.com/tag/microsoft-sql-server/'>Microsoft SQL Server</a>, <a href='http://coldlogics.wordpress.com/tag/select-sql/'>Select (SQL)</a>, <a href='http://coldlogics.wordpress.com/tag/sql/'>SQL</a>, <a href='http://coldlogics.wordpress.com/tag/treeplot/'>TreePlot</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coldlogics.wordpress.com/229/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coldlogics.wordpress.com/229/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coldlogics.wordpress.com&#038;blog=17292051&#038;post=229&#038;subd=coldlogics&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://coldlogics.wordpress.com/2011/05/15/graphing-a-hierarchy-from-a-common-table-expression-using-treeplot/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0fcb3c49aad00374ad5d43b9e6a551cd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">phouseak</media:title>
		</media:content>

		<media:content url="http://coldlogics.files.wordpress.com/2011/05/051511_2244_graphingahi1.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/05/051511_2244_graphingahi2.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/05/051511_2244_graphingahi3.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/05/051511_2244_graphingahi4.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/05/051511_2244_graphingahi5.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/05/051511_2244_graphingahi6.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/05/051511_2244_graphingahi7.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/05/051511_2244_graphingahi8.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/05/051511_2244_graphingahi9.png" medium="image" />

		<media:content url="http://coldlogics.files.wordpress.com/2011/05/051511_2244_graphingahi10.png" medium="image" />
	</item>
	</channel>
</rss>
