<?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/"
	>

<channel>
	<title>Silverlight &#124; WPF &#124; Microsoft.Net &#187; SQL 2005</title>
	<atom:link href="http://joel.neubeck.net/category/microsoft/sql-2005/feed/" rel="self" type="application/rss+xml" />
	<link>http://joel.neubeck.net</link>
	<description>Simplifing structure without changing results</description>
	<lastBuildDate>Wed, 26 May 2010 18:43:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Classification Table &#8211; Part 2</title>
		<link>http://joel.neubeck.net/2007/08/classification-table-part2/</link>
		<comments>http://joel.neubeck.net/2007/08/classification-table-part2/#comments</comments>
		<pubDate>Sat, 18 Aug 2007 15:44:14 +0000</pubDate>
		<dc:creator>joel</dc:creator>
				<category><![CDATA[SQL 2005]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[group-by]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://joel.neubeck.net/2007/08/classification-table-continued/</guid>
		<description><![CDATA[In a recent post I wrote about a flexible structure used for storing and retrieving classifications form a database. My example retrieved “entities” for a single classification, and those entities that matched any of that classifications children. In this entry I want to expand on this approach, and demonstrate how results can be limited to [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent post I wrote about a flexible structure used for storing and retrieving classifications form a database. My example retrieved “entities” for a single classification, and those entities that matched any of that classifications children.</p>
<p>In this entry I want to expand on this approach, and demonstrate how results can be limited to only those entities that match a specific list of classifications.</p>
<p style="”float: left”">
<table border="1">
<tr>
<td><strong>Id</strong></td>
<td><strong>ParentId</strong></td>
<td><strong>Name</strong></td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>Attributes</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>Color</td>
</tr>
<tr>
<td>3</td>
<td>2</td>
<td>Red</td>
</tr>
<tr>
<td>4</td>
<td>2</td>
<td>Blue</td>
</tr>
<tr>
<td>5</td>
<td>2</td>
<td>Green</td>
</tr>
<tr>
<td>6</td>
<td>2</td>
<td>Purple</td>
</tr>
<tr>
<td>7</td>
<td>1</td>
<td>Font</td>
</tr>
<tr>
<td>8</td>
<td>7</td>
<td>Times New Roman</td>
</tr>
<tr>
<td>9</td>
<td>8</td>
<td>Bold</td>
</tr>
<tr>
<td>10</td>
<td>8</td>
<td>Italic</td>
</tr>
<tr>
<td>11</td>
<td>7</td>
<td>Arial</td>
</tr>
<tr>
<td>12</td>
<td>11</td>
<td>Bold</td>
</tr>
<tr>
<td>13</td>
<td>7</td>
<td>Calibri</td>
</tr>
<tr>
<td>14</td>
<td>13</td>
<td>Bold</td>
</tr>
</table>
<p style="”float: left”">
<table border="1">
<tr>
<td><strong>ClassificationId</strong></td>
<td><strong>EntityId</strong></td>
</tr>
<tr>
<td>4</td>
<td>1</td>
</tr>
<tr>
<td>9</td>
<td>1</td>
</tr>
<tr>
<td>4</td>
<td>2</td>
</tr>
<tr>
<td>10</td>
<td>2</td>
</tr>
</table>
<p><br style="”clear: both”" />Using the tables above, let retrieve all entities that are related to ClassificationId 9 (Times New Roman – Bold) AND ClassificationId 4 (Blue).</p>
<p>At first glance I thought this was pretty simple and used the IN clause to match my list of classification ids.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="t-sql" style="font-family:monospace;">SELECT * FROM Entity 
LEFT JOIN EntryClassification 
ON Entity.Id = EntryClassification.EntityId 
WHERE EntryClassification.ClassificationId in (9,4)</pre></td></tr></table></div>

<p>Unfortunately this approach fails to return the correct result set.  Instead of retrieving those entities that are related to BOTH classifications, the IN clause <span style="font-size: 12pt; font-family: 'Georgia','serif'">determines </span>whether a specified value matches ANY value in the list. </p>
<p>After quite a few experimentations , here is the solution I came up with.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="t-sql" style="font-family:monospace;">SELECT * FROM Entity WHERE Entity.Id in 
(SELECT ec.EntityId FROM EntityClassification ec
WHERE ec.ClassificationId in (9,4) 
GROUP BY ec.EntryId 
HAVING Count(ec.EntryId) = 2))</pre></td></tr></table></div>

<p>If the number of items in your list will change, then you will need to either pass in the number of items or calculate the total by examining the string of Ids.  If you always know the delimiter, the following is an interesting hack to <span style="font-size: 12pt; font-family: 'Georgia','serif'">determine </span>the number of items.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="t-sql" style="font-family:monospace;">DECLARE @ClassificationIds varchar(10) = '9,4'
DECLARE @Count int
SET @Count = (LEN(@ClassificationIds ) - LEN(REPLACE(@ClassificationIds , ',', ''))+1)</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://joel.neubeck.net/2007/08/classification-table-part2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Classification Table &#8211; Part 1</title>
		<link>http://joel.neubeck.net/2007/08/classification-table-part1/</link>
		<comments>http://joel.neubeck.net/2007/08/classification-table-part1/#comments</comments>
		<pubDate>Wed, 08 Aug 2007 22:21:46 +0000</pubDate>
		<dc:creator>joel</dc:creator>
				<category><![CDATA[SQL 2005]]></category>
		<category><![CDATA[CTE]]></category>
		<category><![CDATA[reqursive-query]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[table-valued]]></category>

		<guid isPermaLink="false">http://joel.neubeck.net/2007/08/classification-table-design/</guid>
		<description><![CDATA[Frequently our team is tasked with creating a way to categorize data. One very common and flexible approach is to create a table which contains a series of parent-child relationships. This hierarchical approach can be extremely flexible and efficient for categorizing multiple levels of content. Here is an example of a table used to store [...]]]></description>
			<content:encoded><![CDATA[<p>Frequently our team is tasked with creating a way to categorize data. One very common and flexible approach is to create a table which contains a series of parent-child relationships. This hierarchical approach can be extremely flexible and efficient for categorizing multiple levels of content. Here is an example of a table used to store this type of classification.</p>
<table border="1">
<tr>
<td><strong>Id</strong></td>
<td><strong>ParentId</strong></td>
<td><strong>Name</strong></td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>Attributes</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>Color</td>
</tr>
<tr>
<td>3</td>
<td>2</td>
<td>Red</td>
</tr>
<tr>
<td>4</td>
<td>2</td>
<td>Blue</td>
</tr>
<tr>
<td>5</td>
<td>2</td>
<td>Green</td>
</tr>
<tr>
<td>6</td>
<td>2</td>
<td>Purple</td>
</tr>
<tr>
<td>7</td>
<td>1</td>
<td>Font</td>
</tr>
<tr>
<td>8</td>
<td>7</td>
<td>Times New Roman</td>
</tr>
<tr>
<td>9</td>
<td>8</td>
<td>Bold</td>
</tr>
<tr>
<td>10</td>
<td>8</td>
<td>Italic</td>
</tr>
<tr>
<td>11</td>
<td>7</td>
<td>Arial</td>
</tr>
<tr>
<td>12</td>
<td>11</td>
<td>Bold</td>
</tr>
<tr>
<td>13</td>
<td>7</td>
<td>Calibri</td>
</tr>
<tr>
<td>14</td>
<td>13</td>
<td>Bold</td>
</tr>
</table>
<p>From this table we can visualize a simple nested tree of classifications</p>
<ul>
<li>Attributes
<ul>
<li>Color
<ul>
<li>Red</li>
<li>Blue</li>
<li>Green</li>
<li>Purple</li>
</ul>
</li>
</ul>
<ul>
<li>Font
<ul>
<li>Times New Roman
<ul>
<li>Bold</li>
<li>Italic</li>
</ul>
</li>
</ul>
<ul>
<li>Arial
<ul>
<li>Bold</li>
<li>Calibri</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>From a SQL perspective, interacting with this structure is quite painless. Through the use of a composite table, this structure lends itself well to having entities with multiple classification.</p>
<table border="1">
<tr>
<td><strong>ClassificationId</strong></td>
<td><strong>EntityId</strong></td>
</tr>
<tr>
<td>4</td>
<td>1</td>
</tr>
<tr>
<td>9</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>2</td>
</tr>
<tr>
<td>10</td>
<td>2</td>
</tr>
</table>
<p>If a user wants entities which are classified as “Times New Roman” “Bold” they restrict the results to those with a ClassificationId = 9.</p>
<p>Now what if they want are those entities which have any of the “Font” sub classifications (ClassificationId = 7,8,9,10,11,12,13,14)?</p>
<p>This is where it gets a bit more tricky. The approach I have found most flexible is to create a custom table-valued function which recursively walks the table and returns all children of the specified parent.  The results of this reqursive query are returned in the form of a temporary named result set, known as a common table expression (CTE). If our desire was to return more than just the classifications Id we could use this CTE as a sub query on an additional query.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="t-sql" style="font-family:monospace;">ALTER FUNCTION [Resource].[ClassificationChildren]
(
 @ClassificationId int
)
RETURNS TABLE
AS
RETURN
( WITH Classes (Id, ParentId, [Level]) AS
 (
  -- Create the anchor query. This establishes the starting point
  SELECT c.[Id],c.[ParentId],0
  FROM Classification c
  WHERE c.[Id] = @CategoryId
  UNION ALL
  -- Create the recursive query. This query will be executed
  -- until it returns no more rows
  SELECT
  c.[Id],c.[ParentId], b.[Level]+1
  FROM Classification c
  INNER JOIN Classes b ON COALESCE(c.[ParentId],0) = b.[Id]
 )
SELECT k.Id FROM Classes k
)</pre></td></tr></table></div>

<p>Once I have this function, I can create a query to give me the results I desire.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="t-sql" style="font-family:monospace;">SELECT * FROM Entity LEFT JOIN 
EntryClassification ON Entity.Id = EntryClassification.EntityId
WHERE EntryClassification.ClassificationId in 
(SELECT Id FROM ClassificationChildren(7))</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://joel.neubeck.net/2007/08/classification-table-part1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>T-SQL Data formating</title>
		<link>http://joel.neubeck.net/2007/07/t-sql-data-formating/</link>
		<comments>http://joel.neubeck.net/2007/07/t-sql-data-formating/#comments</comments>
		<pubDate>Tue, 17 Jul 2007 22:48:06 +0000</pubDate>
		<dc:creator>joel</dc:creator>
				<category><![CDATA[SQL 2005]]></category>
		<category><![CDATA[Convert]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://joel.neubeck.us/2007/07/t-sql-data-formating/</guid>
		<description><![CDATA[Today I was tasked with formating a SQL Datetime field as part of a FOR XML query.  More often then not, I can get away with formating in the presentation layer and leave all dates in the standard ISO8500.  Today I needed a nice looking date to be concatenated within a business name.  I stumbled across this approach which [...]]]></description>
			<content:encoded><![CDATA[<p>Today I was tasked with formating a SQL Datetime field as part of a FOR XML query.  More often then not, I can get away with formating in the presentation layer and leave all dates in the standard ISO8500.  Today I needed a nice looking date to be concatenated within a business name.  I stumbled across this approach which uses the Convert method within SQL 2005.</p>
<ul>
<li>MON DD YYYY HH:MIAM (OR PM) &#8212; Feb 5 2003 5:54AM<br />
<strong>CONVERT(CHAR(19),GETDATE())</strong></li>
<li>MM-DD-YY FORMAT &#8212; 02-05-03<br />
<strong>CONVERT(CHAR(8),GETDATE(),10)</strong></li>
<li>MM-DD-YYYY FORMAT &#8212; 02-05-2003<br />
<strong>CONVERT(CHAR(10),GETDATE(),110)</strong></li>
<li>DD MON YYYY FORMAT &#8212; 05 Feb 2003<br />
<strong>CONVERT(CHAR(11),GETDATE(),106)</strong></li>
<li>DD MON YY FORMAT &#8212; 05 Feb 03<br />
<strong>CONVERT(CHAR(9),GETDATE(),6)</strong></li>
<li>DD MON YYYY HH:MM:SS:MMM(24H) &#8212; 05 Feb 2003 05:54:39:567<br />
<strong>CONVERT(CHAR(24),GETDATE(),113)</strong></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://joel.neubeck.net/2007/07/t-sql-data-formating/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
