<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Recursive Common Table Expressions</title>
	<atom:link href="http://www.thycotic.com/recursive-common-table-expressions/feed" rel="self" type="application/rss+xml" />
	<link>http://www.thycotic.com/recursive-common-table-expressions</link>
	<description></description>
	<lastBuildDate>Thu, 11 Mar 2010 16:14:45 -0500</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.3</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: IanicBass</title>
		<link>http://www.thycotic.com/recursive-common-table-expressions/comment-page-1#comment-3910</link>
		<dc:creator>IanicBass</dc:creator>
		<pubDate>Mon, 16 Nov 2009 23:42:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.thycotic.com/?p=387#comment-3910</guid>
		<description>Kevin,

Yes, you are correct. In the sample above, CTE would be the best approach.

But just an FYI, we can also just add a where clause in my query and have the same result as yours...

select * from OrganizationUnits where OrganizationUnitId = 3
union all
select s1.* 
from OrganizationUnits s1
inner join OrganizationUnits s2 on s1.ParentOrganizationUnitId = s2.OrganizationUnitId
WHERE 
s2.OrganizationUnitId = 3 OR s2.ParentOrganizationUnitId = 3

Thanks.</description>
		<content:encoded><![CDATA[<p>Kevin,</p>
<p>Yes, you are correct. In the sample above, CTE would be the best approach.</p>
<p>But just an FYI, we can also just add a where clause in my query and have the same result as yours&#8230;</p>
<p>select * from OrganizationUnits where OrganizationUnitId = 3<br />
union all<br />
select s1.*<br />
from OrganizationUnits s1<br />
inner join OrganizationUnits s2 on s1.ParentOrganizationUnitId = s2.OrganizationUnitId<br />
WHERE<br />
s2.OrganizationUnitId = 3 OR s2.ParentOrganizationUnitId = 3</p>
<p>Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin J.</title>
		<link>http://www.thycotic.com/recursive-common-table-expressions/comment-page-1#comment-3909</link>
		<dc:creator>Kevin J.</dc:creator>
		<pubDate>Mon, 16 Nov 2009 23:24:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.thycotic.com/?p=387#comment-3909</guid>
		<description>@IanicBass - Consider the following sample data with the same schema as in my post:

1 &#124; World        &#124; NULL &#124; 1
2 &#124; USA          &#124; 1    &#124; 1
3 &#124; China        &#124; 1    &#124; 1
4 &#124; Fujian       &#124; 3    &#124; 1
5 &#124; Longyan      &#124; 4    &#124; 1
6 &#124; Virgina      &#124; 2    &#124; 1
7 &#124; Falls Church &#124; 6    &#124; 1
8 &#124; California   &#124; 2    &#124; 1
9 &#124; Los Angeles  &#124; 8    &#124; 1

And lets say we wanted to get all children for &quot;World&quot;. That should return everything, right?

Mine as expected returns all rows if I specify 1 as who I want to get the children for. Yours also returns all because it unions all. If I wanted to get JUST the children for China, and ran yours like this:

select * from OrganizationUnits where OrganizationUnitId = 3
union all
select s1.*
from OrganizationUnits s1
inner join OrganizationUnits s2 on s1.ParentOrganizationUnitId = s2.OrganizationUnitId

It would still return everything, including USA which is not a child of China. However using a CTE will properly return 

3  &#124; China    &#124; 1  &#124; 1
4  &#124; Fujian   &#124; 3  &#124; 1
5  &#124; Longyan  &#124; 4  &#124; 1</description>
		<content:encoded><![CDATA[<p>@IanicBass &#8211; Consider the following sample data with the same schema as in my post:</p>
<p>1 | World        | NULL | 1<br />
2 | USA          | 1    | 1<br />
3 | China        | 1    | 1<br />
4 | Fujian       | 3    | 1<br />
5 | Longyan      | 4    | 1<br />
6 | Virgina      | 2    | 1<br />
7 | Falls Church | 6    | 1<br />
8 | California   | 2    | 1<br />
9 | Los Angeles  | 8    | 1</p>
<p>And lets say we wanted to get all children for &#8220;World&#8221;. That should return everything, right?</p>
<p>Mine as expected returns all rows if I specify 1 as who I want to get the children for. Yours also returns all because it unions all. If I wanted to get JUST the children for China, and ran yours like this:</p>
<p>select * from OrganizationUnits where OrganizationUnitId = 3<br />
union all<br />
select s1.*<br />
from OrganizationUnits s1<br />
inner join OrganizationUnits s2 on s1.ParentOrganizationUnitId = s2.OrganizationUnitId</p>
<p>It would still return everything, including USA which is not a child of China. However using a CTE will properly return </p>
<p>3  | China    | 1  | 1<br />
4  | Fujian   | 3  | 1<br />
5  | Longyan  | 4  | 1</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: IanicBass</title>
		<link>http://www.thycotic.com/recursive-common-table-expressions/comment-page-1#comment-3905</link>
		<dc:creator>IanicBass</dc:creator>
		<pubDate>Fri, 13 Nov 2009 23:24:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.thycotic.com/?p=387#comment-3905</guid>
		<description>Kevin, could you please give me an example data where I would not know how deep would the nesting of the children go? I&#039;ve tested this using the query I did with nesting child relationship and it seems to work just fine.

Thank you.</description>
		<content:encoded><![CDATA[<p>Kevin, could you please give me an example data where I would not know how deep would the nesting of the children go? I&#8217;ve tested this using the query I did with nesting child relationship and it seems to work just fine.</p>
<p>Thank you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin J.</title>
		<link>http://www.thycotic.com/recursive-common-table-expressions/comment-page-1#comment-3897</link>
		<dc:creator>Kevin J.</dc:creator>
		<pubDate>Wed, 11 Nov 2009 18:42:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.thycotic.com/?p=387#comment-3897</guid>
		<description>@IanicBass - The problem with that is it isn&#039;t recursive. If you don&#039;t know the depth of the parent child relationship then you would have to keep adding joins for each potential child of each item. This will work regardless of how deep the &quot;nesting&quot; of the children go.</description>
		<content:encoded><![CDATA[<p>@IanicBass &#8211; The problem with that is it isn&#8217;t recursive. If you don&#8217;t know the depth of the parent child relationship then you would have to keep adding joins for each potential child of each item. This will work regardless of how deep the &#8220;nesting&#8221; of the children go.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: IanicBass</title>
		<link>http://www.thycotic.com/recursive-common-table-expressions/comment-page-1#comment-3896</link>
		<dc:creator>IanicBass</dc:creator>
		<pubDate>Wed, 11 Nov 2009 18:25:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.thycotic.com/?p=387#comment-3896</guid>
		<description>You can do this also in a more simplier way...

select * from OrganizationUnits where OrganizationUnitId = 1
union all
select s1.* 
from OrganizationUnits s1
inner join OrganizationUnits s2 on s1.ParentOrganizationUnitId = s2.OrganizationUnitId</description>
		<content:encoded><![CDATA[<p>You can do this also in a more simplier way&#8230;</p>
<p>select * from OrganizationUnits where OrganizationUnitId = 1<br />
union all<br />
select s1.*<br />
from OrganizationUnits s1<br />
inner join OrganizationUnits s2 on s1.ParentOrganizationUnitId = s2.OrganizationUnitId</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dhananjay Goyani</title>
		<link>http://www.thycotic.com/recursive-common-table-expressions/comment-page-1#comment-3885</link>
		<dc:creator>Dhananjay Goyani</dc:creator>
		<pubDate>Fri, 06 Nov 2009 06:25:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.thycotic.com/?p=387#comment-3885</guid>
		<description>Good one...
I had touched the topic - you may want to check the solution.

http://dgoyani.blogspot.com/2005/09/effective-way-to-expand-data-tree-in.html</description>
		<content:encoded><![CDATA[<p>Good one&#8230;<br />
I had touched the topic &#8211; you may want to check the solution.</p>
<p><a href="http://dgoyani.blogspot.com/2005/09/effective-way-to-expand-data-tree-in.html" rel="nofollow">http://dgoyani.blogspot.com/2005/09/effective-way-to-expand-data-tree-in.html</a></p>
]]></content:encoded>
	</item>
</channel>
</rss>
