<?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; group-by</title>
	<atom:link href="http://joel.neubeck.net/tag/group-by/feed/" rel="self" type="application/rss+xml" />
	<link>http://joel.neubeck.net</link>
	<description>Simplifing structure without changing results</description>
	<lastBuildDate>Fri, 01 Apr 2011 21:34:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</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>
	</channel>
</rss>

