<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www2.sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Adam Machanic : T-SQL, Hierarchies</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/tags/T-SQL/Hierarchies/default.aspx</link><description>Tags: T-SQL, Hierarchies</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Swinging From Tree to Tree Using CTEs, Part 2: Adjacency to Nested Intervals</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/swinging-from-tree-to-tree-using-ctes-part-2-adjacency-to-nested-intervals.aspx</link><pubDate>Thu, 13 Jul 2006 01:49:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:105</guid><dc:creator>Adam Machanic</dc:creator><slash:comments>4</slash:comments><comments>http://www2.sqlblog.com/blogs/adam_machanic/comments/105.aspx</comments><wfw:commentRss>http://www2.sqlblog.com/blogs/adam_machanic/commentrss.aspx?PostID=105</wfw:commentRss><description>In our &lt;a href="http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/swinging-from-tree-to-tree-using-ctes-part-1-adjacency-to-nested-sets.aspx"&gt;previous installment&lt;/a&gt;, we saw how to convert Adjacency Lists into Nested Sets using a CTE.&lt;br&gt;


&lt;br&gt;


In this episode, we will convert the Adjacency List into a &lt;a href="http://www.dbazine.com/oracle/or-articles/tropashko4"&gt;Nested Intervals&lt;/a&gt;
encoding.&amp;nbsp; Specifically, this encoding will make use of the Nested
Intervals with Continued Fractions technique that Tropashko presented
in a &lt;a href="http://arxiv.org/ftp/cs/papers/0402/0402051.pdf"&gt;later paper&lt;/a&gt;.&lt;br&gt;


&lt;br&gt;


The key to this technique lies in using a slightly different form of
materialized path than was used in the last post.&amp;nbsp; Rather than
materializing the EmployeeIds into a path, the path will be created as
an &lt;i&gt;enumerated&lt;/i&gt;
representation, based on sibling ordering.&amp;nbsp; For example, the first
two levels of the AdventureWorks HumanResources.Employee table's tree
look like:&lt;br&gt;


&lt;br&gt;


&lt;blockquote&gt;
EmployeeId 109 (CEO)&lt;br&gt;
  &lt;blockquote&gt;EmployeeId 6 (Marketing Manager)&lt;br&gt;
EmployeeId 12 (VP Engineering)&lt;br&gt;
EmployeeId 42 (IS Manager)&lt;br&gt;
EmployeeId 140 (CFO)&lt;br&gt;
EmployeeId 148 (VP Production)&lt;br&gt;
EmployeeId 273 (VP Sales)&lt;br&gt;
    &lt;br&gt;
  &lt;/blockquote&gt;
&lt;/blockquote&gt;


Using the materialized path representation from the previous post, the paths to the second-level employees would be:&lt;br&gt;


&lt;br&gt;


&lt;blockquote&gt;6: 109.6&lt;br&gt;
12: 109.12&lt;br&gt;
42: 109.42&lt;br&gt;
140: 109.140&lt;br&gt;
148: 109.148&lt;br&gt;
273: 109.273&lt;br&gt;
  &lt;br&gt;
&lt;/blockquote&gt;


However, for this post, paths will instead be materialized based on
sibling ordering.&amp;nbsp; We don't know anything about how siblings
should be ordered in the AdventureWorks employee hierarchy (that's
probably a business rule question, if it matters at all).&amp;nbsp; So
ordering will be done by EmployeeId:&lt;br&gt;


&lt;br&gt;


&lt;blockquote&gt;6: 1.1&lt;br&gt;
12: 1.2&lt;br&gt;
42: 1.3&lt;br&gt;
140: 1.4&lt;br&gt;
148: 1.5&lt;br&gt;
273: 1.6&lt;br&gt;
&lt;/blockquote&gt;


The significance of this encoding is that for each path a rational
number can be generated, using a Euclidian algorithm (described very
well on &lt;a href="http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/cfINTRO.html"&gt;this web site&lt;/a&gt;).&amp;nbsp;
The algorithm works by iterating over each element on the path,
building a rational number as it goes.&amp;nbsp; The beauty of this, as
shown by Tropashko in his paper, is that by using these paths we can
determine an interval, beween which all children of a given path will
fall.&lt;br&gt;


&lt;br&gt;

In order to accomplish getting the path, the ROW_NUMBER function
will be used, but slightly differently than last time.&amp;nbsp; Instead of
creating a second CTE to reference the first, thereby getting the row
number for each element of the path, the ROW_NUMBER function will be
embedded within the recursive CTE itself, as in the following example:&lt;br&gt;


&lt;blockquote&gt;&lt;font face="Courier New"&gt;WITH EmployeeRows AS&lt;br&gt;
(&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; EmployeeId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ManagerId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ROW_NUMBER() OVER (ORDER BY EmployeeId) AS theRow&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM HumanResources.Employee&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE ManagerId IS NULL&lt;br&gt;
  &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; UNION ALL&lt;br&gt;
  &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; e.EmployeeId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; e.ManagerId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ROW_NUMBER() OVER (ORDER BY e.EmployeeId) AS theRow&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM EmployeeRows x&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; JOIN HumanResources.Employee e ON e.ManagerId = x.EmployeeId&lt;br&gt;
)&lt;br&gt;
SELECT *&lt;br&gt;
FROM EmployeeRows&lt;br&gt;
ORDER BY &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ManagerId, &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; EmployeeId&lt;/font&gt;&lt;br&gt;
&lt;/blockquote&gt;


Interestingly, this example as-is will return &lt;i&gt;exactly the same results&lt;/i&gt; as the following, non-recursive CTE example:&lt;br&gt;


&lt;blockquote&gt;&lt;font face="Courier New"&gt;SELECT&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; EmployeeId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ManagerId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ROW_NUMBER() OVER (PARTITION BY ManagerId ORDER BY EmployeeId) AS theRow&lt;br&gt;
FROM HumanResources.Employee&lt;br&gt;
ORDER BY &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ManagerId, &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; EmployeeId&lt;br&gt;
  &lt;/font&gt;&lt;/blockquote&gt;


So, why do we care?&amp;nbsp; Take a close look at the two examples.&amp;nbsp;
In the CTE example, the ROW_NUMBER function does not use PARTITION
BY.&amp;nbsp; Yet, results are &lt;i&gt;implicitly&lt;/i&gt; partitioned.&amp;nbsp; This
gives an interesting view into the inner-workings of CTEs.&amp;nbsp; As it
turns out, the recursive part of the CTE is called once per row
returned by the anchor or previous recursion.&amp;nbsp; This is not how I
originally expected CTEs to behave (I thought the recursive part would
be called once per rowset returned by the anchor or previous
recursion), but it does help us with this particular task!&lt;br&gt;


&lt;br&gt;


The current row number, at any given point in the recursion, represents
the enumeration for that node.&amp;nbsp; But because we're using a
recursive CTE, we also have access to the parent's enumaration.&amp;nbsp;
For instance, to build an enumerated path, the following T-SQL would be
used:&lt;br&gt;


&lt;blockquote&gt;&lt;font face="Courier New"&gt;WITH EmployeeRows AS&lt;br&gt;
(&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; EmployeeId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ManagerId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(VARCHAR(MAX), ROW_NUMBER() OVER (ORDER BY EmployeeId)) AS thePath&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM HumanResources.Employee&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE ManagerId IS NULL&lt;br&gt;
  &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; UNION ALL&lt;br&gt;
  &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; e.EmployeeId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; e.ManagerId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x.thePath + '.' +
CONVERT(VARCHAR(MAX), ROW_NUMBER() OVER (ORDER BY e.EmployeeId)) AS
thePath&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM EmployeeRows x&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; JOIN HumanResources.Employee e ON e.ManagerId = x.EmployeeId&lt;br&gt;
)&lt;br&gt;
SELECT *&lt;br&gt;
FROM EmployeeRows&lt;br&gt;
ORDER BY &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; thePath&lt;br&gt;
  &lt;/font&gt;&lt;/blockquote&gt;


Note that this sample can be made a bit more readable (and more
functional for later) by embeding the ROW_NUMBER in a derived table:&lt;br&gt;


&lt;blockquote&gt;&lt;font face="Courier New"&gt;WITH EmployeeRows AS&lt;br&gt;
(&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; y.EmployeeId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; y.ManagerId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(VARCHAR(MAX), y.theRow) AS thePath&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; EmployeeId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ManagerId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ROW_NUMBER() OVER (ORDER BY EmployeeId) AS theRow&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; FROM HumanResources.Employee&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE ManagerId IS NULL&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ) y&lt;br&gt;
  &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; UNION ALL&lt;br&gt;
  &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; y.EmployeeId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; y.ManagerId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; y.thePath + '.' + CONVERT(VARCHAR(MAX), y.theRow) AS thePath&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; e.EmployeeId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; e.ManagerId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x.thePath,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ROW_NUMBER() OVER (ORDER BY e.EmployeeId) AS theRow&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; FROM EmployeeRows x&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; JOIN HumanResources.Employee e ON e.ManagerId = x.EmployeeId&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ) y&lt;br&gt;
)&lt;br&gt;
SELECT *&lt;br&gt;
FROM EmployeeRows&lt;br&gt;
ORDER BY &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; thePath&lt;br&gt;
  &lt;/font&gt;&lt;/blockquote&gt;


From here, it's a simple step to implement the Euclidian algorithm.&amp;nbsp; The algorithm is quite simple:&lt;br&gt;


&lt;ol&gt;&lt;li&gt;Set parentNumerator &amp;lt;- 1&lt;/li&gt;&lt;li&gt;Set parentDenominator &amp;lt;- 0&lt;/li&gt;&lt;li&gt;Set theElement &amp;lt;- first enumeration in the path&lt;/li&gt;&lt;li&gt;Set currentNumerator &amp;lt;- theElement&lt;/li&gt;&lt;li&gt;Set currentDenominator &amp;lt;- 1&lt;/li&gt;&lt;li&gt;Set theElement &amp;lt;- next enumeration in the path&lt;/li&gt;&lt;li&gt;Set previousParentNumerator &amp;lt;- parentNumerator&lt;/li&gt;&lt;li&gt;Set previousParentDenominator &amp;lt;- parentDenominator&lt;/li&gt;&lt;li&gt;Set parentNumerator &amp;lt;- currentNumerator&lt;/li&gt;&lt;li&gt;Set parentDenominator &amp;lt;- currentDenominator&lt;/li&gt;&lt;li&gt;Set currentNumerator &amp;lt;- (parentNumerator * theElement) + previousParentNumerator&lt;/li&gt;&lt;li&gt;Set currentDenominator &amp;lt;- (parentDenominator * theElement) + previousParentDenominator&lt;/li&gt;&lt;li&gt;If the current element is not the final node in the path, goto 6.&lt;/li&gt;&lt;/ol&gt;

This seems a bit hairy, but I think that looking at the algorithm
and spending a few minutes with our old friends pencil and paper will
make it quite clear.&amp;nbsp; Also, look at the web site linked
above.&amp;nbsp; Here is how I've implemented the algorithm using a CTE:&lt;br&gt;


&lt;blockquote&gt;&lt;font face="Courier New"&gt;WITH EmployeeRows AS&lt;br&gt;

(&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; EmployeeId,&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(VARCHAR(MAX), theRow) AS thePath,&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(BIGINT, 1) AS prevNumer,&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(BIGINT, 0) AS prevDenom,&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(BIGINT, theRow) AS currNumer,&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(BIGINT, 1) AS currDenom&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; EmployeeId,&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ROW_NUMBER() OVER (ORDER BY EmployeeId) AS theRow&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; FROM HumanResources.Employee&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE ManagerId IS NULL&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; ) y&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; UNION ALL&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; y.EmployeeId,&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; y.thePath + '.' + CONVERT(VARCHAR(MAX), y.theRow) AS thePath,&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; prevNumer = y.currNumer,&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; prevDenom = y.currDenom,&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; (y.currNumer * y.theRow) + y.prevNumer&amp;nbsp; AS currNumer,&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; (y.currDenom * y.theRow) + y.prevDenom&amp;nbsp; AS currDenom&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT &lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; e.EmployeeID,&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x.thePath,&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x.currNumer,&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x.currDenom,&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x.prevNumer,&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x.prevDenom,&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ROW_NUMBER() OVER (ORDER BY e.EmployeeID) AS therow&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; FROM EmployeeRows x&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; JOIN HumanResources.Employee e ON e.ManagerId = x.EmployeeId&lt;br&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp; ) y&lt;br&gt;

)&lt;br&gt;
  &lt;/font&gt;&lt;/blockquote&gt;

Note that in this case I didn't require the use
of the temporary variables; the previous anchor/recursive parts act as
temporary storage enough.&amp;nbsp; &lt;br&gt;


&lt;br&gt;


Readers will also hopefully notice that I haven't yet included a SELECT
to get the data from the CTE!&amp;nbsp; This is because I'd like to explain
briefly what it will do.&amp;nbsp; In his paper, Tropashko explains that for
each node, the intervals for the children of that node will fall into
an interval between the encoding of that node (currNumer / currDenom)
and the numerator of the previous node plus the numerator for the
current node, divided by the denominator of the previous node plus the
denominator for the current node ((currNumer + prevNumer) / (currDenom
+ prevDenom)).&amp;nbsp; Quite wordy here.&amp;nbsp; Refer to the paper for a better
explanation and proof.&lt;br&gt;


&lt;blockquote&gt;
&lt;/blockquote&gt;



Anyway, the completed query follows:&lt;br&gt;


&lt;blockquote&gt;&lt;font face="Courier New"&gt;WITH EmployeeRows AS&lt;br&gt;
(&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; EmployeeId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(VARCHAR(MAX), theRow) AS thePath,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(BIGINT, 1) AS prevNumer,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(BIGINT, 0) AS prevDenom,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(BIGINT, theRow) AS currNumer,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(BIGINT, 1) AS currDenom&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; EmployeeId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ROW_NUMBER() OVER (ORDER BY EmployeeId) AS theRow&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; FROM HumanResources.Employee&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE ManagerId IS NULL&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ) y&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; UNION ALL&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; y.EmployeeId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; y.thePath + '.' + CONVERT(VARCHAR(MAX), y.theRow) AS thePath,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; prevNumer = y.currNumer,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; prevDenom = y.currDenom,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; (y.currNumer * y.theRow) + y.prevNumer&amp;nbsp; AS currNumer,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; (y.currDenom * y.theRow) + y.prevDenom&amp;nbsp; AS currDenom&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; e.EmployeeID,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x.thePath,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x.currNumer,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x.currDenom,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x.prevNumer,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x.prevDenom,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ROW_NUMBER() OVER (ORDER BY e.EmployeeID) AS therow&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; FROM EmployeeRows x&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; JOIN HumanResources.Employee e ON e.ManagerId = x.EmployeeId&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ) y&lt;br&gt;
)&lt;br&gt;
SELECT&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; EmployeeId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; thePath,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; currNumer AS startNumer,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; currDenom AS startDenom,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; currNumer + prevNumer AS endNumer,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; currDenom + prevDenom AS endDenom&lt;br&gt;
FROM EmployeeRows&lt;br&gt;
  &lt;/font&gt;&lt;/blockquote&gt;


For each node (EmployeeId), you now have an interval (start and end)
within which all children intervals will fall.&amp;nbsp; Note that computation
must be done by the interval, not by the current node's encoding.&amp;nbsp; The
reason becomes apparent when looking at the encodings for 1.1.1 and
1.2.&amp;nbsp; They are the same; however, their intervals do not overlap.&amp;nbsp; As a
matter of fact, encodings will be the same for every next sibling/first
child pair.&amp;nbsp; But the intervals remain nested, and if proper queries are
written there will be no confusion.&lt;br&gt;


&lt;br&gt;


So that's a first step towards using the Nested Intervals Model in SQL

Server 2005.&amp;nbsp; Stay tuned for more... And as always, feel free to post
questions or comments.&amp;nbsp; I know some of this material can be confusing
(at least, it was to me before I wrote this post!)&lt;br&gt;&lt;br&gt;&lt;img src="http://www2.sqlblog.com/aggbug.aspx?PostID=105" width="1" height="1"&gt;</description><category domain="http://www2.sqlblog.com/blogs/adam_machanic/archive/tags/T-SQL/default.aspx">T-SQL</category><category domain="http://www2.sqlblog.com/blogs/adam_machanic/archive/tags/Hierarchies/default.aspx">Hierarchies</category></item><item><title>Swinging From Tree to Tree Using CTEs, Part 1: Adjacency to Nested Sets</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/swinging-from-tree-to-tree-using-ctes-part-1-adjacency-to-nested-sets.aspx</link><pubDate>Thu, 13 Jul 2006 01:47:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:103</guid><dc:creator>Adam Machanic</dc:creator><slash:comments>19</slash:comments><comments>http://www2.sqlblog.com/blogs/adam_machanic/comments/103.aspx</comments><wfw:commentRss>http://www2.sqlblog.com/blogs/adam_machanic/commentrss.aspx?PostID=103</wfw:commentRss><description>I'm not sure how many times over the last several years I've seen the
same tired article titles... "Climbing Trees in SQL," "Climbing Up the
SQL Tree," or maybe, "Naked Coeds Playing in the Trees!" ... Oh
wait, I think that last one might be something else.&lt;br&gt;

&lt;br&gt;

But anyway, the point is, I'm going to adhere to that standard.&amp;nbsp;
But I'm adding a Tarzan-esque theme to this post, because we're not
going to just climb a tree.&amp;nbsp; We're going to swing about between
trees.&amp;nbsp; Different types of tree representations are appropriate
for different scenarios.&amp;nbsp; Which is why, as I pointed out in my &lt;a href="http://searchsqlserver.techtarget.com/tip/1,289483,sid87_gci1107414,00.html?FromTaxonomy=/pr/301329"&gt;recent SearchSQLServer article on recursive Common Table Expressions&lt;/a&gt;,
we have so many different ways of representing them.&amp;nbsp; Adjacency
Lists, Materialized Paths, Nested Sets, and Nested Intervals spring to
mind.&amp;nbsp; And there are probably others.&lt;br&gt;

&lt;br&gt;

My article shows how to use the CTEs, in conjunction with a dynamically
generated materialized path, to manipulate an Adjacency List, getting
many of the benefits associated with using the Nested Sets Model.&amp;nbsp;
And that's great.&amp;nbsp; But the Nested Sets Model itself might be
useful in your endeavors.&amp;nbsp; So in this post, I will show how to
extend the CTE discussed in that article.&lt;br&gt;

&lt;br&gt;

The end-result CTE in the article, which can be run in the
AdventureWorks database, looks something like this (renamed for the
sake of this post):&lt;br&gt;

&lt;br&gt;

&lt;blockquote&gt;&lt;font face="Courier New"&gt;WITH EmployeeLevels AS&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;
(&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; EmployeeId,&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(VARCHAR(MAX), EmployeeId) AS thePath,&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 1 AS Level&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM HumanResources.Employee&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE ManagerId IS NULL&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;
  &lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; UNION ALL&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;
  &lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; e.EmployeeId,&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x.thePath + '.' + CONVERT(VARCHAR(MAX), e.EmployeeId) AS thePath,&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x.Level + 1 AS Level&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM EmployeeLevels x&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; JOIN HumanResources.Employee e on e.ManagerId = x.EmployeeId&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;
)&lt;/font&gt;&lt;br&gt;
&lt;/blockquote&gt;

thePath is the materialized path, a '.' delimited breadcrumb from the
root to each node.&amp;nbsp; We also end up with a level, which represents
how many nodes away from the root in the hierarchy each employee sits
(very important for those upwardly-mobile junior execs, no doubt!)&lt;br&gt;

&lt;br&gt;

I'm going to assume that readers of this post are familiar with the &lt;a href="http://www.intelligententerprise.com/001020/celko.jhtml?_requestid=235427"&gt;Nested Sets Model&lt;/a&gt;, as popularized by Joe Celko.&amp;nbsp; If not, read the link.&lt;br&gt;

&lt;br&gt;

After staring at the Nested Sets for a while, I arrived at the following mathematical properties:&lt;br&gt;

&lt;br&gt;

&lt;ul&gt;&lt;li&gt;Value of Lft for the root node is 1&lt;/li&gt;&lt;li&gt;Value of Rgt for the root node is 2 * (Number of nodes)&lt;/li&gt;&lt;li&gt;Value of Lft for any node is ((Number of nodes visited) * 2) - (Level of current node)&lt;/li&gt;&lt;li&gt;Value of Rgt for any node is (Lft value) + ((Number of subnodes) * 2) + 1&lt;/li&gt;&lt;/ul&gt;
I think the only factor here that requires further explanation is
"(Number of nodes visited)".&amp;nbsp; By this, I mean the number of
nodes that would have been visited (including the current node) if one
were doing a
preorder traversal of the tree. Luckily, this number is quite easy
to determine.&amp;nbsp; The row number for each row, as determined by
ordering by the materialized path, &lt;i&gt;is&lt;/i&gt;
this number.&amp;nbsp; I
encourage readers to validate this with pencil and paper if it doesn't
quite make sense reading on the screen.&amp;nbsp; Draw a simple tree and
traverse, starting from the lefthand side of the root node.&lt;br&gt;

&lt;br&gt;
But how to translate that into T-SQL?&amp;nbsp; Luckily, SQL Server 2005, in addition to CTEs, also includes the very useful &lt;a href="http://blogs.conchango.com/jamiethomson/archive/2005/02/16/1025.aspx"&gt;ROW_NUMBER() function&lt;/a&gt;.&amp;nbsp;
So to get the row number, we simply need to add another CTE into the
chain (did you know that successive CTEs can use the results of
previous CTEs?):&lt;br&gt;

&lt;br&gt;

&lt;blockquote&gt;&lt;font face="Courier New"&gt;WITH EmployeeLevels AS&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;(&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; EmployeeId,&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(VARCHAR(MAX), EmployeeId) AS thePath,&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 1 AS Level&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM HumanResources.Employee&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE ManagerId IS NULL&lt;/font&gt;&lt;br&gt;
  &lt;br&gt;
  &lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; UNION ALL&lt;/font&gt;&lt;br&gt;
  &lt;br&gt;
  &lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; e.EmployeeId,&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x.thePath + '.' + CONVERT(VARCHAR(MAX), e.EmployeeId) AS thePath,&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x.Level + 1 AS Level&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM EmployeeLevels x&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; JOIN HumanResources.Employee e on e.ManagerId = x.EmployeeId&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;),&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;EmployeeRows AS&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;(&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;EmployeeLevels.*,&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ROW_NUMBER() OVER (ORDER BY thePath) AS Row&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM EmployeeLevels&lt;/font&gt;&lt;br&gt;
  &lt;font face="Courier New"&gt;)&lt;br&gt;
  &lt;/font&gt;&lt;/blockquote&gt;

We now have current level and number of nodes visited, which gives us
the Lft value for each node.&amp;nbsp; But how to determine the number of
subnodes, in order to get the Rgt value?&amp;nbsp; Luckily, the materialized
path also gives us that capability...&lt;br&gt;

&lt;br&gt;

For any given node, number of subnodes can be determined by counting
all nodes whose path value is LIKE the current path value + '.%'.&lt;br&gt;

&lt;br&gt;

The resultant query should be fairly obvious at this point:&lt;br&gt;

&lt;br&gt;

&lt;blockquote&gt;&lt;font face="Courier New"&gt;WITH EmployeeLevels AS&lt;br&gt;
(&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; EmployeeId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CONVERT(VARCHAR(MAX), EmployeeId) AS thePath,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 1 AS Level&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM HumanResources.Employee&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE ManagerId IS NULL&lt;br&gt;
  &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; UNION ALL&lt;br&gt;
  &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; e.EmployeeId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x.thePath + '.' + CONVERT(VARCHAR(MAX), e.EmployeeId) AS thePath,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x.Level + 1 AS Level&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM EmployeeLevels x&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; JOIN HumanResources.Employee e on e.ManagerId = x.EmployeeId&lt;br&gt;
),&lt;br&gt;
EmployeeRows AS&lt;br&gt;
(&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;EmployeeLevels.*,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ROW_NUMBER() OVER (ORDER BY thePath) AS Row&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM EmployeeLevels&lt;br&gt;
)&lt;br&gt;
SELECT&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ER.EmployeeId,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ER.thePath,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ER.Level,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ER.Row,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;(ER.Row * 2) - ER.Level AS Lft,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;((ER.Row * 2) - ER.Level) + &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT COUNT(*) * 2&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; FROM EmployeeRows ER2 &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE ER2.thePath LIKE ER.thePath + '.%'&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ) + 1 AS Rgt&lt;br&gt;
FROM EmployeeRows ER&lt;br&gt;
ORDER BY thePath&lt;br&gt;
  &lt;/font&gt;&lt;/blockquote&gt;

... And that's it!&amp;nbsp; We have now converted the Adjacency List tree into a Nested Sets tree.&lt;br&gt;

&lt;br&gt;

In the next installment, I will show how to determine the Nested
Intervals encoding from an Adjacency List tree, also using recursive
CTEs.&lt;br&gt;

&lt;br&gt;

Questions?&lt;br&gt;

&lt;br&gt;&lt;br&gt;&lt;img src="http://www2.sqlblog.com/aggbug.aspx?PostID=103" width="1" height="1"&gt;</description><category domain="http://www2.sqlblog.com/blogs/adam_machanic/archive/tags/T-SQL/default.aspx">T-SQL</category><category domain="http://www2.sqlblog.com/blogs/adam_machanic/archive/tags/Hierarchies/default.aspx">Hierarchies</category></item></channel></rss>