<?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/"
	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>Comments on: SQL SERVER &#8211; Simple Example of Recursive CTE</title>
	<atom:link href="http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Wed, 22 May 2013 19:03:49 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Bhavik Shah</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-454569</link>
		<dc:creator><![CDATA[Bhavik Shah]]></dc:creator>
		<pubDate>Fri, 12 Apr 2013 15:24:49 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-454569</guid>
		<description><![CDATA[Hi Pinal,
the above example can be done even with joins and groupby. how about the below be more apt example for cte?

with CI AS (SELECT t.name,t.object_id,count(t.object_id) CI
				FROM sys.indexes i inner join sys.tables t
				on i.object_id = t.object_id
				where t.type like &#039;u&#039; and i.type_desc like &#039;CLUSTERED&#039;
				group by t.name,t.object_id)
	, NCI AS (SELECT t.name,t.object_id,count(t.object_id) NCI
				FROM sys.indexes i inner join sys.tables t
				on i.object_id = t.object_id
				where t.type like &#039;u&#039; and i.type_desc like &#039;NONCLUSTERED&#039;
				group by t.name,t.object_id)
SELECT t.name,CI.CI,NCI.NCI,CI.CI + NCI.NCI TI
FROM sys.tables t inner join CI on CI.Object_id = t.object_id
inner join NCI on NCI.object_id = t.object_id
where t.type like &#039;u&#039;
order by TI desc

select t.name,count(i.index_id) 
from sys.tables t inner join sys.indexes i on i.object_id = t.object_id 
where i.type_desc in (&#039;CLUSTERED&#039;,&#039;NONCLUSTERED&#039;)
group by t.name
order by 2 desc]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal,<br />
the above example can be done even with joins and groupby. how about the below be more apt example for cte?</p>
<p>with CI AS (SELECT t.name,t.object_id,count(t.object_id) CI<br />
				FROM sys.indexes i inner join sys.tables t<br />
				on i.object_id = t.object_id<br />
				where t.type like &#8216;u&#8217; and i.type_desc like &#8216;CLUSTERED&#8217;<br />
				group by t.name,t.object_id)<br />
	, NCI AS (SELECT t.name,t.object_id,count(t.object_id) NCI<br />
				FROM sys.indexes i inner join sys.tables t<br />
				on i.object_id = t.object_id<br />
				where t.type like &#8216;u&#8217; and i.type_desc like &#8216;NONCLUSTERED&#8217;<br />
				group by t.name,t.object_id)<br />
SELECT t.name,CI.CI,NCI.NCI,CI.CI + NCI.NCI TI<br />
FROM sys.tables t inner join CI on CI.Object_id = t.object_id<br />
inner join NCI on NCI.object_id = t.object_id<br />
where t.type like &#8216;u&#8217;<br />
order by TI desc</p>
<p>select t.name,count(i.index_id)<br />
from sys.tables t inner join sys.indexes i on i.object_id = t.object_id<br />
where i.type_desc in (&#8216;CLUSTERED&#8217;,'NONCLUSTERED&#8217;)<br />
group by t.name<br />
order by 2 desc</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arun</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-441591</link>
		<dc:creator><![CDATA[Arun]]></dc:creator>
		<pubDate>Thu, 21 Mar 2013 11:57:57 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-441591</guid>
		<description><![CDATA[Hello,

I have to use table valued function twice in a same SELECT statement. So will l SQL retrieve data once or twice if the same function is used in two places?

If it retrieves twice will CET resolves it?
Thank you!]]></description>
		<content:encoded><![CDATA[<p>Hello,</p>
<p>I have to use table valued function twice in a same SELECT statement. So will l SQL retrieve data once or twice if the same function is used in two places?</p>
<p>If it retrieves twice will CET resolves it?<br />
Thank you!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ravikumar</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-428992</link>
		<dc:creator><![CDATA[ravikumar]]></dc:creator>
		<pubDate>Thu, 28 Feb 2013 06:19:39 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-428992</guid>
		<description><![CDATA[i have a table called test :- create table test (id int , Docname varchar(200), parentkey int, ContentType varchar(200))
insert into test values (1,&#039;ParentFolder&#039;,-3,&#039;Folder&#039;)
insert into test values (2,&#039;ChildFolder1&#039;,1,&#039;Folder&#039;)
insert into test values ( 3,&#039;ChildFolder1&#039;,2,&#039;Folder&#039;)
insert into test values (4,&#039;ParentFolder 2&#039;,-3,&#039;Folder&#039;)
insert into test values (5,&#039;test Folder 1&#039;,4,&#039;Folder&#039;)
insert into test values (6,&#039;test Folder 2&#039;,4,&#039;Folder&#039;)

in this table id is the primarykey, i have a column called parentkey in this table, in this table records having parent-child relation

ex:- id  Name  PrentKey
      1  test  -3
      2  test2   1

-3 is the parentkey here, 1 vlaue in the parentkey is the child of  id 1,

here if i give child id key i want to get the all names of that child n parent fileds. example :- if i give &#039;2&#039; as input parament i want out put like  below :
 
 ID   Path
       1-2 test-test2

for this i m trying below query :-


  WITH Emp_CTE ( id,ParentKey,ContentType,Path,Level)  
  AS (  

   SELECT id , ParentKey,ContentType,
   CONVERT(varchar(1000),DocName) , 0 Level   
   FROM test  S  
   WHERE id = 2  

   UNION ALL  

   SELECT  SS.ID , SS.ParentKey,SS.ContentType,
   CONVERT(varchar(1000), ISNULL(SS.DocName,&#039;&#039;)+&#039;&#124;&#039;+ISNULL(Path,&#039;&#039;) ) 
   ,Level +1  
   FROM test   SS  
   INNER JOIN Emp_CTE ecte ON ecte.ParentKey = SS.ID  
  )  

  SELECT  CASE WHEN Path IS NULL THEN &#039;&#039; ELSE &#039;&#124;&#039; END + Path as folderpath,
    * FROM Emp_CTE  WHERE ContentType = &#039;Folder&#039;  AND ParentKey = -3

ablove query is giving output as below :-

   ID   Path
         1 test&#124;test2

here i want to display all the IDs , but i am able to append all the docnames from table , how to achive id&#039;s like 1-2?]]></description>
		<content:encoded><![CDATA[<p>i have a table called test :- create table test (id int , Docname varchar(200), parentkey int, ContentType varchar(200))<br />
insert into test values (1,&#8217;ParentFolder&#8217;,-3,&#8217;Folder&#8217;)<br />
insert into test values (2,&#8217;ChildFolder1&#8242;,1,&#8217;Folder&#8217;)<br />
insert into test values ( 3,&#8217;ChildFolder1&#8242;,2,&#8217;Folder&#8217;)<br />
insert into test values (4,&#8217;ParentFolder 2&#8242;,-3,&#8217;Folder&#8217;)<br />
insert into test values (5,&#8217;test Folder 1&#8242;,4,&#8217;Folder&#8217;)<br />
insert into test values (6,&#8217;test Folder 2&#8242;,4,&#8217;Folder&#8217;)</p>
<p>in this table id is the primarykey, i have a column called parentkey in this table, in this table records having parent-child relation</p>
<p>ex:- id  Name  PrentKey<br />
      1  test  -3<br />
      2  test2   1</p>
<p>-3 is the parentkey here, 1 vlaue in the parentkey is the child of  id 1,</p>
<p>here if i give child id key i want to get the all names of that child n parent fileds. example :- if i give &#8217;2&#8242; as input parament i want out put like  below :</p>
<p> ID   Path<br />
       1-2 test-test2</p>
<p>for this i m trying below query :-</p>
<p>  WITH Emp_CTE ( id,ParentKey,ContentType,Path,Level)<br />
  AS (  </p>
<p>   SELECT id , ParentKey,ContentType,<br />
   CONVERT(varchar(1000),DocName) , 0 Level<br />
   FROM test  S<br />
   WHERE id = 2  </p>
<p>   UNION ALL  </p>
<p>   SELECT  SS.ID , SS.ParentKey,SS.ContentType,<br />
   CONVERT(varchar(1000), ISNULL(SS.DocName,&#8221;)+&#8217;|'+ISNULL(Path,&#8221;) )<br />
   ,Level +1<br />
   FROM test   SS<br />
   INNER JOIN Emp_CTE ecte ON ecte.ParentKey = SS.ID<br />
  )  </p>
<p>  SELECT  CASE WHEN Path IS NULL THEN &#8221; ELSE &#8216;|&#8217; END + Path as folderpath,<br />
    * FROM Emp_CTE  WHERE ContentType = &#8216;Folder&#8217;  AND ParentKey = -3</p>
<p>ablove query is giving output as below :-</p>
<p>   ID   Path<br />
         1 test|test2</p>
<p>here i want to display all the IDs , but i am able to append all the docnames from table , how to achive id&#8217;s like 1-2?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pravesh</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-417731</link>
		<dc:creator><![CDATA[pravesh]]></dc:creator>
		<pubDate>Mon, 04 Feb 2013 16:43:48 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-417731</guid>
		<description><![CDATA[Great Pinal!
You are god of sql]]></description>
		<content:encoded><![CDATA[<p>Great Pinal!<br />
You are god of sql</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nick - SQL Developer</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-409012</link>
		<dc:creator><![CDATA[Nick - SQL Developer]]></dc:creator>
		<pubDate>Thu, 17 Jan 2013 16:23:23 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-409012</guid>
		<description><![CDATA[...btw...This is how I was approching it prior to hearing I should use a recursive query.

PRINT&#039;List all Wiz DBs; Lar_Names, DB_Name, Types, CensusYear&#039;

SELECT  
NodeName, 
NodeId,
LEN(NodePath) - LEN(REPLACE(NodePath, &#039;.&#039;, &#039;&#039;)) AS DCount,  
NodePath, 
Left(objectstore..LAR_Tree.NodeName,40)AS Lar_Name, 
Left(objectstore..LAR_Items.DatabaseName,10) AS Database_Name, 
objectstore..LAR_Items.LARType, objectstore..LAR_Items.CensusYear
INTO #tbl_Temp
FROM objectstore..LAR_Tree 
Left JOIN objectstore..LAR_Items 
ON objectstore..LAR_Tree.NodeId = objectstore..LAR_Items.ItemId
ORDER BY  Database_Name

SELECT 
NodePath,
CensusYear, 
Database_Name,
(CASE 
	WHEN DCount = 3 THEN (CASE 
									WHEN LEN(NodePath) = 5 THEN
										(SELECT NodeName
										FROM objectstore..LAR_Tree
										WHERE NodeId = CAST(SUBSTRING(NodePath,4,1)AS INT))
									WHEN LEN(NodePath) = 6 THEN
										(SELECT NodeName
										FROM objectstore..LAR_Tree
										WHERE NodeId = CAST(SUBSTRING(NodePath,4,2)AS INT))
									WHEN LEN(NodePath) = 7 THEN
										(SELECT NodeName
										FROM objectstore..LAR_Tree
										WHERE NodeId = CAST(SUBSTRING(NodePath,4,3)AS INT))
									WHEN LEN(NodePath) = 8 THEN
										(SELECT NodeName
										FROM objectstore..LAR_Tree
										WHERE NodeId = CAST(SUBSTRING(NodePath,4,4)AS INT))
							END)

END) AS GUI_Path
FROM #tbl_Temp]]></description>
		<content:encoded><![CDATA[<p>&#8230;btw&#8230;This is how I was approching it prior to hearing I should use a recursive query.</p>
<p>PRINT&#8217;List all Wiz DBs; Lar_Names, DB_Name, Types, CensusYear&#8217;</p>
<p>SELECT<br />
NodeName,<br />
NodeId,<br />
LEN(NodePath) &#8211; LEN(REPLACE(NodePath, &#8216;.&#8217;, &#8221;)) AS DCount,<br />
NodePath,<br />
Left(objectstore..LAR_Tree.NodeName,40)AS Lar_Name,<br />
Left(objectstore..LAR_Items.DatabaseName,10) AS Database_Name,<br />
objectstore..LAR_Items.LARType, objectstore..LAR_Items.CensusYear<br />
INTO #tbl_Temp<br />
FROM objectstore..LAR_Tree<br />
Left JOIN objectstore..LAR_Items<br />
ON objectstore..LAR_Tree.NodeId = objectstore..LAR_Items.ItemId<br />
ORDER BY  Database_Name</p>
<p>SELECT<br />
NodePath,<br />
CensusYear,<br />
Database_Name,<br />
(CASE<br />
	WHEN DCount = 3 THEN (CASE<br />
									WHEN LEN(NodePath) = 5 THEN<br />
										(SELECT NodeName<br />
										FROM objectstore..LAR_Tree<br />
										WHERE NodeId = CAST(SUBSTRING(NodePath,4,1)AS INT))<br />
									WHEN LEN(NodePath) = 6 THEN<br />
										(SELECT NodeName<br />
										FROM objectstore..LAR_Tree<br />
										WHERE NodeId = CAST(SUBSTRING(NodePath,4,2)AS INT))<br />
									WHEN LEN(NodePath) = 7 THEN<br />
										(SELECT NodeName<br />
										FROM objectstore..LAR_Tree<br />
										WHERE NodeId = CAST(SUBSTRING(NodePath,4,3)AS INT))<br />
									WHEN LEN(NodePath) = 8 THEN<br />
										(SELECT NodeName<br />
										FROM objectstore..LAR_Tree<br />
										WHERE NodeId = CAST(SUBSTRING(NodePath,4,4)AS INT))<br />
							END)</p>
<p>END) AS GUI_Path<br />
FROM #tbl_Temp</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nick - SQL Developer</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-409008</link>
		<dc:creator><![CDATA[Nick - SQL Developer]]></dc:creator>
		<pubDate>Thu, 17 Jan 2013 16:19:46 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-409008</guid>
		<description><![CDATA[I am trying to extract numeric values from a field (NodePath) that are seperated by decimal points.  For example, .1.2.56.4235.8571.  each numeric value represents a unique identifyer (NodeId) for a folder or file in an application.  The db I&#039;m pulling the data from (objectstore..Lar_Tree) has the NodeId (int), the NodePath (varchar(900)) and the NodeName (varchar(128)).  Once I have the numeric values extracted I want to concatenate them into a path for documentation.  Someone suggested using a recursive query, but I have not written one yet and can&#039;t seem to get it to work based on the example above.  Any help would be greatly appreciated.]]></description>
		<content:encoded><![CDATA[<p>I am trying to extract numeric values from a field (NodePath) that are seperated by decimal points.  For example, .1.2.56.4235.8571.  each numeric value represents a unique identifyer (NodeId) for a folder or file in an application.  The db I&#8217;m pulling the data from (objectstore..Lar_Tree) has the NodeId (int), the NodePath (varchar(900)) and the NodeName (varchar(128)).  Once I have the numeric values extracted I want to concatenate them into a path for documentation.  Someone suggested using a recursive query, but I have not written one yet and can&#8217;t seem to get it to work based on the example above.  Any help would be greatly appreciated.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Koti rddy</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-377389</link>
		<dc:creator><![CDATA[Koti rddy]]></dc:creator>
		<pubDate>Tue, 20 Nov 2012 11:40:40 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-377389</guid>
		<description><![CDATA[Can You tell me the query How to find out what are the child tables  suppose if I have given table Name 
Example Suppose If I have given table name like emp
I need to find out what are the child tables for employeeclass]]></description>
		<content:encoded><![CDATA[<p>Can You tell me the query How to find out what are the child tables  suppose if I have given table Name<br />
Example Suppose If I have given table name like emp<br />
I need to find out what are the child tables for employeeclass</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sandeep</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-362829</link>
		<dc:creator><![CDATA[Sandeep]]></dc:creator>
		<pubDate>Mon, 22 Oct 2012 05:21:31 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-362829</guid>
		<description><![CDATA[Hi Gautam,
have you got answer for this question....?
Please , help me out, if you got answer]]></description>
		<content:encoded><![CDATA[<p>Hi Gautam,<br />
have you got answer for this question&#8230;.?<br />
Please , help me out, if you got answer</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sonali</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-360255</link>
		<dc:creator><![CDATA[Sonali]]></dc:creator>
		<pubDate>Mon, 15 Oct 2012 10:26:54 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-360255</guid>
		<description><![CDATA[Hi Pinal,
I need to get userdetails from mytable who havent done any transactions in last  
N Months . transaction details and userdetails are maintained in different tables.]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal,<br />
I need to get userdetails from mytable who havent done any transactions in last<br />
N Months . transaction details and userdetails are maintained in different tables.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: syedbarkath</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-354833</link>
		<dc:creator><![CDATA[syedbarkath]]></dc:creator>
		<pubDate>Mon, 01 Oct 2012 09:31:40 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-354833</guid>
		<description><![CDATA[Create table Doc
(
DocNum int,
DocEntry int
)
insert into Doc values(1,234),(2,324),(2,746),(3,876),(3,764),(4,100),(4,387)

Select DocNUm
,left(DocEntry,len(DocEntry)-1)DocEntry 
from(
Select distinct DocNum
,(
Select Cast(DocEntry as varchar)+&#039;,&#039; from Doc B where A.DocNum=B.DocNum for xml path(&#039;&#039;) 
 )DocEntry from Doc A
 )C]]></description>
		<content:encoded><![CDATA[<p>Create table Doc<br />
(<br />
DocNum int,<br />
DocEntry int<br />
)<br />
insert into Doc values(1,234),(2,324),(2,746),(3,876),(3,764),(4,100),(4,387)</p>
<p>Select DocNUm<br />
,left(DocEntry,len(DocEntry)-1)DocEntry<br />
from(<br />
Select distinct DocNum<br />
,(<br />
Select Cast(DocEntry as varchar)+&#8217;,&#8217; from Doc B where A.DocNum=B.DocNum for xml path(&#8221;)<br />
 )DocEntry from Doc A<br />
 )C</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: commissary</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-341041</link>
		<dc:creator><![CDATA[commissary]]></dc:creator>
		<pubDate>Wed, 05 Sep 2012 07:39:11 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-341041</guid>
		<description><![CDATA[hi pinal can u please revert back with a query to find a palindrome of a string using CTE recursive function 
thanks in advance. .
really need it]]></description>
		<content:encoded><![CDATA[<p>hi pinal can u please revert back with a query to find a palindrome of a string using CTE recursive function<br />
thanks in advance. .<br />
really need it</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: darpan</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-310057</link>
		<dc:creator><![CDATA[darpan]]></dc:creator>
		<pubDate>Fri, 06 Jul 2012 05:51:16 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-310057</guid>
		<description><![CDATA[I found query but it use 3 select query but to improve performance is there any way so i can retrieve answer less than 3 select query? Either by with cte or rownumber or rank or partition by?]]></description>
		<content:encoded><![CDATA[<p>I found query but it use 3 select query but to improve performance is there any way so i can retrieve answer less than 3 select query? Either by with cte or rownumber or rank or partition by?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Veeren</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-300313</link>
		<dc:creator><![CDATA[Veeren]]></dc:creator>
		<pubDate>Tue, 12 Jun 2012 09:03:46 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-300313</guid>
		<description><![CDATA[R u able to resolve this?]]></description>
		<content:encoded><![CDATA[<p>R u able to resolve this?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: aman</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-284583</link>
		<dc:creator><![CDATA[aman]]></dc:creator>
		<pubDate>Mon, 14 May 2012 19:51:17 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-284583</guid>
		<description><![CDATA[Hello,

I have a requirment where the hierarchy structure is defined in the below way.

QA Sponsor 
QA Fund 1 
QA Fund 2 
QA Fund 3 
QA Fund 4

QA Fund 5 
QA Fund 1

Below is the proposed db design.

Child Id Level Parent
QA Sponsor 1 
QA Fund1 2 QA Sponsor
QA Fund 2 3 QA Fund1
QA Fund 3 4 QA Fund 2
QA Fund 4 5 QA Fund 3

QA Fund 5 2 QA Sponsor
QA Fund1 2 QA Fund 5

I have written the below CTE code and which provide me the o/p which is mentioned after the code.

;WITH cte AS
(
SELECT CAST(&#039;&#039; + Name AS VARCHAR(100)) as &#039;Name&#039;, ID
FROM dbo.RELATION
WHERE PARENT_ID =&#039;&#039;

UNION ALL

SELECT CAST(cte.Name + &#039;---&gt;&#039; + t.Name AS VARCHAR(100)), t.ID
FROM dbo.RELATION t
INNER JOIN cte ON t.parent_id = cte.id
)
SELECT Name FROM cte
ORDER BY ID

O/P
---

QA SPONSOR
QA SPONSOR---&gt;QA FUND1
QA SPONSOR---&gt;QA FUND1---&gt;QA FUND2
QA SPONSOR---&gt;QA FUND1---&gt;QA FUND2---&gt;QA FUND3
QA SPONSOR---&gt;QA FUND1---&gt;QA FUND2---&gt;QA FUND3---&gt;QA FUND4
QA SPONSOR---&gt;QA FUND5
QA SPONSOR---&gt;QA FUND5---&gt;QA FUND1

Everything looks ok however but since QA Fund 1 has fund 2,3,and 4 inside it. there should be one more line i.e. QA SPONSOR---&gt;QA FUND5---&gt;QA FUND1---&gt;QA FUND2---&gt;QA FUND3---&gt;QA FUND4

I am stuck and not sure what should i do to get around this problem. Please help me in this.]]></description>
		<content:encoded><![CDATA[<p>Hello,</p>
<p>I have a requirment where the hierarchy structure is defined in the below way.</p>
<p>QA Sponsor<br />
QA Fund 1<br />
QA Fund 2<br />
QA Fund 3<br />
QA Fund 4</p>
<p>QA Fund 5<br />
QA Fund 1</p>
<p>Below is the proposed db design.</p>
<p>Child Id Level Parent<br />
QA Sponsor 1<br />
QA Fund1 2 QA Sponsor<br />
QA Fund 2 3 QA Fund1<br />
QA Fund 3 4 QA Fund 2<br />
QA Fund 4 5 QA Fund 3</p>
<p>QA Fund 5 2 QA Sponsor<br />
QA Fund1 2 QA Fund 5</p>
<p>I have written the below CTE code and which provide me the o/p which is mentioned after the code.</p>
<p>;WITH cte AS<br />
(<br />
SELECT CAST(&#8221; + Name AS VARCHAR(100)) as &#8216;Name&#8217;, ID<br />
FROM dbo.RELATION<br />
WHERE PARENT_ID =&#8221;</p>
<p>UNION ALL</p>
<p>SELECT CAST(cte.Name + &#8216;&#8212;&gt;&#8217; + t.Name AS VARCHAR(100)), t.ID<br />
FROM dbo.RELATION t<br />
INNER JOIN cte ON t.parent_id = cte.id<br />
)<br />
SELECT Name FROM cte<br />
ORDER BY ID</p>
<p>O/P<br />
&#8212;</p>
<p>QA SPONSOR<br />
QA SPONSOR&#8212;&gt;QA FUND1<br />
QA SPONSOR&#8212;&gt;QA FUND1&#8212;&gt;QA FUND2<br />
QA SPONSOR&#8212;&gt;QA FUND1&#8212;&gt;QA FUND2&#8212;&gt;QA FUND3<br />
QA SPONSOR&#8212;&gt;QA FUND1&#8212;&gt;QA FUND2&#8212;&gt;QA FUND3&#8212;&gt;QA FUND4<br />
QA SPONSOR&#8212;&gt;QA FUND5<br />
QA SPONSOR&#8212;&gt;QA FUND5&#8212;&gt;QA FUND1</p>
<p>Everything looks ok however but since QA Fund 1 has fund 2,3,and 4 inside it. there should be one more line i.e. QA SPONSOR&#8212;&gt;QA FUND5&#8212;&gt;QA FUND1&#8212;&gt;QA FUND2&#8212;&gt;QA FUND3&#8212;&gt;QA FUND4</p>
<p>I am stuck and not sure what should i do to get around this problem. Please help me in this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Reena</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-277511</link>
		<dc:creator><![CDATA[Reena]]></dc:creator>
		<pubDate>Thu, 19 Apr 2012 09:35:52 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-277511</guid>
		<description><![CDATA[Good article...]]></description>
		<content:encoded><![CDATA[<p>Good article&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cOLL</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-272442</link>
		<dc:creator><![CDATA[cOLL]]></dc:creator>
		<pubDate>Thu, 05 Apr 2012 11:54:22 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-272442</guid>
		<description><![CDATA[CREATE TABLE [dbo].[Jogo](
	[Jogo] [float] NULL,
	[Casa] [float] NULL,
	[Coluna1] [nvarchar](3) NULL,
	[Coluna2] [nvarchar](3) NULL,
	[Coluna3] [nvarchar](3) NULL
) ON [PRIMARY]

GO


Jogo,Casa,Coluna1,Coluna2,Coluna3
255,1,C1:,NULL,NULL
255,2,A1:,B1:,C1:
255,3,A1:,NULL,NULL
255,4,A1:,C1:,NULL
255,5,A1:,NULL,NULL
255,6,C1:,B1:,NULL
255,7,A1:,C1:,NULL
255,8,A1:,NULL,NULL
255,9,A1:,NULL,NULL
255,10,A1:,B1:,C1:
255,11,A1:,NULL,NULL
255,12,C1:,NULL,NULL
255,13,A1:,NULL,NULL
255,14,A1:,C1:,NULL
255,15,A1:,NULL,NULL




Jogo	Casa	Coluna1	Coluna2	Coluna3
255	1	C1:	NULL	NULL
255	2	A1:	B1:	C1:
255	3	A1:	NULL	NULL
255	4	A1:	C1:	NULL
255	5	A1:	NULL	NULL
255	6	C1:	B1:	NULL
255	7	A1:	C1:	NULL
255	8	A1:	NULL	NULL
255	9	A1:	NULL	NULL
255	10	A1:	B1:	C1:
255	11	A1:	NULL	NULL
255	12	C1:	NULL	NULL
255	13	A1:	NULL	NULL
255	14	A1:	C1:	NULL
255	15	A1:	NULL	NULL





Using SQL generate all possible between the columns 1, 2 and 3.
 This is an example of practical utilization of query recurssive applied to sports lottery (loteca).
 The result should be 144 bet as element 15 in the column should be based on CASA.


FIRST LINE == B1:A1:A1:A1:A1:B1:A1:A1:A1:A1:A1:B1:A1:A1:A1:
LAST LINE ==   B1:B1:A1:B1:A1:CA1::B1:A1:A1:B1:A1:B1:A1:B1:A1:]]></description>
		<content:encoded><![CDATA[<p>CREATE TABLE [dbo].[Jogo](<br />
	[Jogo] [float] NULL,<br />
	[Casa] [float] NULL,<br />
	[Coluna1] [nvarchar](3) NULL,<br />
	[Coluna2] [nvarchar](3) NULL,<br />
	[Coluna3] [nvarchar](3) NULL<br />
) ON [PRIMARY]</p>
<p>GO</p>
<p>Jogo,Casa,Coluna1,Coluna2,Coluna3<br />
255,1,C1:,NULL,NULL<br />
255,2,A1:,B1:,C1:<br />
255,3,A1:,NULL,NULL<br />
255,4,A1:,C1:,NULL<br />
255,5,A1:,NULL,NULL<br />
255,6,C1:,B1:,NULL<br />
255,7,A1:,C1:,NULL<br />
255,8,A1:,NULL,NULL<br />
255,9,A1:,NULL,NULL<br />
255,10,A1:,B1:,C1:<br />
255,11,A1:,NULL,NULL<br />
255,12,C1:,NULL,NULL<br />
255,13,A1:,NULL,NULL<br />
255,14,A1:,C1:,NULL<br />
255,15,A1:,NULL,NULL</p>
<p>Jogo	Casa	Coluna1	Coluna2	Coluna3<br />
255	1	C1:	NULL	NULL<br />
255	2	A1:	B1:	C1:<br />
255	3	A1:	NULL	NULL<br />
255	4	A1:	C1:	NULL<br />
255	5	A1:	NULL	NULL<br />
255	6	C1:	B1:	NULL<br />
255	7	A1:	C1:	NULL<br />
255	8	A1:	NULL	NULL<br />
255	9	A1:	NULL	NULL<br />
255	10	A1:	B1:	C1:<br />
255	11	A1:	NULL	NULL<br />
255	12	C1:	NULL	NULL<br />
255	13	A1:	NULL	NULL<br />
255	14	A1:	C1:	NULL<br />
255	15	A1:	NULL	NULL</p>
<p>Using SQL generate all possible between the columns 1, 2 and 3.<br />
 This is an example of practical utilization of query recurssive applied to sports lottery (loteca).<br />
 The result should be 144 bet as element 15 in the column should be based on CASA.</p>
<p>FIRST LINE == B1:A1:A1:A1:A1:B1:A1:A1:A1:A1:A1:B1:A1:A1:A1:<br />
LAST LINE ==   B1:B1:A1:B1:A1:CA1::B1:A1:A1:B1:A1:B1:A1:B1:A1:</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SURA</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-265934</link>
		<dc:creator><![CDATA[SURA]]></dc:creator>
		<pubDate>Thu, 22 Mar 2012 06:09:56 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-265934</guid>
		<description><![CDATA[Hello All

I have two tables like below. This is something like parent child relationship. Table 1 have relation details and Table 2 have ids for each row. 

Table 1

Col1 Col2 col3
A A1 A1.1
B B1 B1.1
B B1 B1.2
C C1 C1.1
C C2 C2.1
C C3 C3.1


Table 2
Col1 KEY
A 1
A1 2
A1.1 3
B 4
B1 5
B1.1 6
B1.2 7
C 8
C1 9
C1.1 10
C2 11
C2.1 12
C3 13
C3.1 14


Extecpted output


CHILD PARENT

14 13
12 11
10 9
13 8
11 8
9 8
7 5
6 5
5 4
3 2
2 1
NULL 1


how to get it using quyer]]></description>
		<content:encoded><![CDATA[<p>Hello All</p>
<p>I have two tables like below. This is something like parent child relationship. Table 1 have relation details and Table 2 have ids for each row. </p>
<p>Table 1</p>
<p>Col1 Col2 col3<br />
A A1 A1.1<br />
B B1 B1.1<br />
B B1 B1.2<br />
C C1 C1.1<br />
C C2 C2.1<br />
C C3 C3.1</p>
<p>Table 2<br />
Col1 KEY<br />
A 1<br />
A1 2<br />
A1.1 3<br />
B 4<br />
B1 5<br />
B1.1 6<br />
B1.2 7<br />
C 8<br />
C1 9<br />
C1.1 10<br />
C2 11<br />
C2.1 12<br />
C3 13<br />
C3.1 14</p>
<p>Extecpted output</p>
<p>CHILD PARENT</p>
<p>14 13<br />
12 11<br />
10 9<br />
13 8<br />
11 8<br />
9 8<br />
7 5<br />
6 5<br />
5 4<br />
3 2<br />
2 1<br />
NULL 1</p>
<p>how to get it using quyer</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Maja Stojanova</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-259072</link>
		<dc:creator><![CDATA[Maja Stojanova]]></dc:creator>
		<pubDate>Sat, 03 Mar 2012 21:35:06 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-259072</guid>
		<description><![CDATA[Hi, can you please explain me why in this example in the recursive member stands p.PerAssemblyQty and not bom.PerAssemblyQty?

WITH Parts (AssemblyID, ComponentID, PerAssemblyQty, EndDate, ComponentLevel) AS
(
SELECT b.ProductAssemblyID, b.ComponentID, b.PerAssemblyQty, b.EndDate, 0 AS ComponentLevel
FROM Production.BillOfMaterials AS b
WHERE b.ProductAssemblyID = 800 AND b.EndDate IS NULL
UNION ALL
SELECT bom.ProductAssemblyID, bom.ComponentID, p.PerAssemblyQty, bom.EndDate, ComponentLevel + 1
FROM Production.BillOfMaterials AS bom
INNER JOIN Parts AS p
ON bom.ProductAssemblyID = p.ComponentID AND bom.EndDate IS NULL
)
SELECT AssemblyID, ComponentID, Name, PerAssemblyQty, EndDate,
        ComponentLevel 
FROM Parts AS p
    INNER JOIN Production.Product AS pr
    ON p.ComponentID = pr.ProductID
ORDER BY ComponentLevel, AssemblyID, ComponentID;
GO

Thank you in advance]]></description>
		<content:encoded><![CDATA[<p>Hi, can you please explain me why in this example in the recursive member stands p.PerAssemblyQty and not bom.PerAssemblyQty?</p>
<p>WITH Parts (AssemblyID, ComponentID, PerAssemblyQty, EndDate, ComponentLevel) AS<br />
(<br />
SELECT b.ProductAssemblyID, b.ComponentID, b.PerAssemblyQty, b.EndDate, 0 AS ComponentLevel<br />
FROM Production.BillOfMaterials AS b<br />
WHERE b.ProductAssemblyID = 800 AND b.EndDate IS NULL<br />
UNION ALL<br />
SELECT bom.ProductAssemblyID, bom.ComponentID, p.PerAssemblyQty, bom.EndDate, ComponentLevel + 1<br />
FROM Production.BillOfMaterials AS bom<br />
INNER JOIN Parts AS p<br />
ON bom.ProductAssemblyID = p.ComponentID AND bom.EndDate IS NULL<br />
)<br />
SELECT AssemblyID, ComponentID, Name, PerAssemblyQty, EndDate,<br />
        ComponentLevel<br />
FROM Parts AS p<br />
    INNER JOIN Production.Product AS pr<br />
    ON p.ComponentID = pr.ProductID<br />
ORDER BY ComponentLevel, AssemblyID, ComponentID;<br />
GO</p>
<p>Thank you in advance</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vick</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-254603</link>
		<dc:creator><![CDATA[Vick]]></dc:creator>
		<pubDate>Mon, 20 Feb 2012 22:29:42 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-254603</guid>
		<description><![CDATA[I still didn&#039;t the solution for this.. Plz help.

Table has following 2 columns

DocNum DocEntry
1 234
2 324
2 746
3 876
3 764
4 100
4 387

Expected result is as follow

1 234
2 324, 746
3 876, 764
4 100, 387]]></description>
		<content:encoded><![CDATA[<p>I still didn&#8217;t the solution for this.. Plz help.</p>
<p>Table has following 2 columns</p>
<p>DocNum DocEntry<br />
1 234<br />
2 324<br />
2 746<br />
3 876<br />
3 764<br />
4 100<br />
4 387</p>
<p>Expected result is as follow</p>
<p>1 234<br />
2 324, 746<br />
3 876, 764<br />
4 100, 387</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER &#8211; Convert Subquery to CTE &#8211; SQL in Sixty Seconds #001 &#8211; Video &#171; SQL Server Journey with SQL Authority</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-249748</link>
		<dc:creator><![CDATA[SQL SERVER &#8211; Convert Subquery to CTE &#8211; SQL in Sixty Seconds #001 &#8211; Video &#171; SQL Server Journey with SQL Authority]]></dc:creator>
		<pubDate>Wed, 08 Feb 2012 01:32:05 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-249748</guid>
		<description><![CDATA[[...] on CTE: Simple Example of Recursive CTE Multiple CTE in One SELECT Statement Query Common Table Expression (CTE) and Few Observation Delete [...]]]></description>
		<content:encoded><![CDATA[<p>[...] on CTE: Simple Example of Recursive CTE Multiple CTE in One SELECT Statement Query Common Table Expression (CTE) and Few Observation Delete [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ramesh :)</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-249523</link>
		<dc:creator><![CDATA[ramesh :)]]></dc:creator>
		<pubDate>Tue, 07 Feb 2012 12:43:00 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-249523</guid>
		<description><![CDATA[Try this:


create table #temp1 (rno int, points varchar(20))
INSERT INTO #temp1 VALUES (1,234)
INSERT INTO #temp1 VALUES(2,324)
INSERT INTO #temp1 VALUES(2,746)
INSERT INTO #temp1 VALUES(3,876)
INSERT INTO #temp1 VALUES(3,764)
INSERT INTO #temp1 VALUES(4,100)
INSERT INTO #temp1 VALUES(4,834)

select * from #temp1


select distinct a.rno,
substring(
(select points + &#039;,&#039; from #temp1 b where a.rno=b.rno order by a.rno for xml path(&#039;&#039;)),0,
LEN(
(select points + &#039;,&#039; from #temp1 b where a.rno=b.rno order by a.rno for xml path(&#039;&#039;)))
)
from #temp1 a]]></description>
		<content:encoded><![CDATA[<p>Try this:</p>
<p>create table #temp1 (rno int, points varchar(20))<br />
INSERT INTO #temp1 VALUES (1,234)<br />
INSERT INTO #temp1 VALUES(2,324)<br />
INSERT INTO #temp1 VALUES(2,746)<br />
INSERT INTO #temp1 VALUES(3,876)<br />
INSERT INTO #temp1 VALUES(3,764)<br />
INSERT INTO #temp1 VALUES(4,100)<br />
INSERT INTO #temp1 VALUES(4,834)</p>
<p>select * from #temp1</p>
<p>select distinct a.rno,<br />
substring(<br />
(select points + &#8216;,&#8217; from #temp1 b where a.rno=b.rno order by a.rno for xml path(&#8221;)),0,<br />
LEN(<br />
(select points + &#8216;,&#8217; from #temp1 b where a.rno=b.rno order by a.rno for xml path(&#8221;)))<br />
)<br />
from #temp1 a</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: satish kumar</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-244472</link>
		<dc:creator><![CDATA[satish kumar]]></dc:creator>
		<pubDate>Fri, 27 Jan 2012 09:58:00 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-244472</guid>
		<description><![CDATA[Thanks Pinal.]]></description>
		<content:encoded><![CDATA[<p>Thanks Pinal.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER &#8211; Common Gotcha&#8217;s Associated with Common Table Expressions (CTE) &#8211; Quiz &#8211; Puzzle &#8211; 26 of 31 &#171; SQL Server Journey with SQL Authority</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-244328</link>
		<dc:creator><![CDATA[SQL SERVER &#8211; Common Gotcha&#8217;s Associated with Common Table Expressions (CTE) &#8211; Quiz &#8211; Puzzle &#8211; 26 of 31 &#171; SQL Server Journey with SQL Authority]]></dc:creator>
		<pubDate>Fri, 27 Jan 2012 01:31:23 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-244328</guid>
		<description><![CDATA[[...] (CTE) and Few Observation Multiple CTE in One SELECT Statement Query Delete Duplicate Rows Simple Example of Recursive CTE SQL SERVER – Simple Example of Recursive CTE – Part 2 – MAXRECURSION – Prevent CTE Infinite [...]]]></description>
		<content:encoded><![CDATA[<p>[...] (CTE) and Few Observation Multiple CTE in One SELECT Statement Query Delete Duplicate Rows Simple Example of Recursive CTE SQL SERVER – Simple Example of Recursive CTE – Part 2 – MAXRECURSION – Prevent CTE Infinite [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sam</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-226895</link>
		<dc:creator><![CDATA[sam]]></dc:creator>
		<pubDate>Tue, 27 Dec 2011 18:18:01 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-226895</guid>
		<description><![CDATA[Please help me in this.. i have written cte but facing problem in achieving my desired output:
organizationtable:
oid, orgname, level, parentid
10-unit1-1-0
11-dept1-2-10
12-dept2-2-10
13-sec1-3-11
empdtls:
eid staffno oid
1-99-13

cte which i have writted is below:
with testorg as(
 select oid,orgname,parentid from #organizationdtls where parentid = 0 
 union all
 select a.oid,a.orgname,a.parentid from #organizationdtls a inner join testorg t on(a.parentid = t.oid)
 )
 select t.* from testorg t  

Help me in achieving such output if i pass oid = 10 then it should return 
empdtls:
eid staffno oid
1-99-13

Please help me  !!]]></description>
		<content:encoded><![CDATA[<p>Please help me in this.. i have written cte but facing problem in achieving my desired output:<br />
organizationtable:<br />
oid, orgname, level, parentid<br />
10-unit1-1-0<br />
11-dept1-2-10<br />
12-dept2-2-10<br />
13-sec1-3-11<br />
empdtls:<br />
eid staffno oid<br />
1-99-13</p>
<p>cte which i have writted is below:<br />
with testorg as(<br />
 select oid,orgname,parentid from #organizationdtls where parentid = 0<br />
 union all<br />
 select a.oid,a.orgname,a.parentid from #organizationdtls a inner join testorg t on(a.parentid = t.oid)<br />
 )<br />
 select t.* from testorg t  </p>
<p>Help me in achieving such output if i pass oid = 10 then it should return<br />
empdtls:<br />
eid staffno oid<br />
1-99-13</p>
<p>Please help me  !!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: diksta33</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-216133</link>
		<dc:creator><![CDATA[diksta33]]></dc:creator>
		<pubDate>Mon, 12 Dec 2011 16:06:23 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-216133</guid>
		<description><![CDATA[Then I thought you probably want the child with the id = 6 to come after its parent in the list, i.e. 1, 4, 6, 5, 2, 7, 8, 3.

This will work for three levels (same table variable as before):

WITH cte AS (
	SELECT 
		idConcept,
		idParentConcept,
		NULL AS ParentParent,
		[Description]
	FROM 
		@Temp
	WHERE 
		idParentConcept IS NULL
UNION ALL
	SELECT 
		t.idConcept,
		t.idParentConcept,
		cte.idParentConcept,
		t.[Description]
	FROM 
		@Temp t
		INNER JOIN cte ON t.idParentConcept = cte.idConcept)
SELECT 
	idConcept,
	idParentConcept,
	[Description]
FROM 
	cte 
ORDER BY
	COALESCE(ParentParent, idParentConcept, idConcept),
	CASE WHEN ParentParent IS NULL THEN idConcept ELSE idParentConcept END,
	idConcept;]]></description>
		<content:encoded><![CDATA[<p>Then I thought you probably want the child with the id = 6 to come after its parent in the list, i.e. 1, 4, 6, 5, 2, 7, 8, 3.</p>
<p>This will work for three levels (same table variable as before):</p>
<p>WITH cte AS (<br />
	SELECT<br />
		idConcept,<br />
		idParentConcept,<br />
		NULL AS ParentParent,<br />
		[Description]<br />
	FROM<br />
		@Temp<br />
	WHERE<br />
		idParentConcept IS NULL<br />
UNION ALL<br />
	SELECT<br />
		t.idConcept,<br />
		t.idParentConcept,<br />
		cte.idParentConcept,<br />
		t.[Description]<br />
	FROM<br />
		@Temp t<br />
		INNER JOIN cte ON t.idParentConcept = cte.idConcept)<br />
SELECT<br />
	idConcept,<br />
	idParentConcept,<br />
	[Description]<br />
FROM<br />
	cte<br />
ORDER BY<br />
	COALESCE(ParentParent, idParentConcept, idConcept),<br />
	CASE WHEN ParentParent IS NULL THEN idConcept ELSE idParentConcept END,<br />
	idConcept;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
