<?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>Thu, 09 Feb 2012 10:31:47 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<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>
	<item>
		<title>By: diksta33</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-216124</link>
		<dc:creator><![CDATA[diksta33]]></dc:creator>
		<pubDate>Mon, 12 Dec 2011 15:45:29 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-216124</guid>
		<description><![CDATA[Try this:

DECLARE @Temp TABLE (idConcept INT, idParentConcept INT, [Description] VARCHAR(50));
INSERT INTO @Temp VALUES (1, NULL, &#039;Root Concept&#039;);
INSERT INTO @Temp VALUES (2, NULL, &#039;Root Concept&#039;);
INSERT INTO @Temp VALUES (3, NULL, &#039;Root Concept&#039;);
INSERT INTO @Temp VALUES (4, 1, &#039;Child Level 1&#039;);
INSERT INTO @Temp VALUES (5, 1, &#039;Child Level 1&#039;);
INSERT INTO @Temp VALUES (6, 4, &#039;Child Level 2&#039;);
INSERT INTO @Temp VALUES (7, 2, &#039;Child Level 1&#039;);
INSERT INTO @Temp VALUES (8, 2, &#039;Child Level 1&#039;);


WITH cte AS (
	SELECT 
		idConcept,
		idParentConcept,
		[Description]
	FROM 
		@Temp
	WHERE 
		idParentConcept IS NULL
UNION ALL
	SELECT 
		t.idConcept,
		t.idParentConcept,
		t.[Description]
	FROM 
		@Temp t
		INNER JOIN cte ON t.idParentConcept = cte.idConcept
)
SELECT 
	c1.*
FROM 
	cte c1
	LEFT JOIN cte c2 ON c2.idConcept = c1.idParentConcept
	LEFT JOIN cte c3 ON c3.idConcept = c2.idParentConcept
ORDER BY
	COALESCE(c3.idConcept, c2.idConcept, c1.idConcept),
	COALESCE(c2.idConcept, c1.idConcept),
	c1.idConcept;

--idConcept	idParentConcept	Description
--1	NULL	Root Concept
--4	1	Child Level 1
--5	1	Child Level 1
--6	4	Child Level 2
--2	NULL	Root Concept
--7	2	Child Level 1
--8	2	Child Level 1
--3	NULL	Root Concept]]></description>
		<content:encoded><![CDATA[<p>Try this:</p>
<p>DECLARE @Temp TABLE (idConcept INT, idParentConcept INT, [Description] VARCHAR(50));<br />
INSERT INTO @Temp VALUES (1, NULL, &#8216;Root Concept&#8217;);<br />
INSERT INTO @Temp VALUES (2, NULL, &#8216;Root Concept&#8217;);<br />
INSERT INTO @Temp VALUES (3, NULL, &#8216;Root Concept&#8217;);<br />
INSERT INTO @Temp VALUES (4, 1, &#8216;Child Level 1&#8242;);<br />
INSERT INTO @Temp VALUES (5, 1, &#8216;Child Level 1&#8242;);<br />
INSERT INTO @Temp VALUES (6, 4, &#8216;Child Level 2&#8242;);<br />
INSERT INTO @Temp VALUES (7, 2, &#8216;Child Level 1&#8242;);<br />
INSERT INTO @Temp VALUES (8, 2, &#8216;Child Level 1&#8242;);</p>
<p>WITH cte AS (<br />
	SELECT<br />
		idConcept,<br />
		idParentConcept,<br />
		[Description]<br />
	FROM<br />
		@Temp<br />
	WHERE<br />
		idParentConcept IS NULL<br />
UNION ALL<br />
	SELECT<br />
		t.idConcept,<br />
		t.idParentConcept,<br />
		t.[Description]<br />
	FROM<br />
		@Temp t<br />
		INNER JOIN cte ON t.idParentConcept = cte.idConcept<br />
)<br />
SELECT<br />
	c1.*<br />
FROM<br />
	cte c1<br />
	LEFT JOIN cte c2 ON c2.idConcept = c1.idParentConcept<br />
	LEFT JOIN cte c3 ON c3.idConcept = c2.idParentConcept<br />
ORDER BY<br />
	COALESCE(c3.idConcept, c2.idConcept, c1.idConcept),<br />
	COALESCE(c2.idConcept, c1.idConcept),<br />
	c1.idConcept;</p>
<p>&#8211;idConcept	idParentConcept	Description<br />
&#8211;1	NULL	Root Concept<br />
&#8211;4	1	Child Level 1<br />
&#8211;5	1	Child Level 1<br />
&#8211;6	4	Child Level 2<br />
&#8211;2	NULL	Root Concept<br />
&#8211;7	2	Child Level 1<br />
&#8211;8	2	Child Level 1<br />
&#8211;3	NULL	Root Concept</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nilesh</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-216088</link>
		<dc:creator><![CDATA[nilesh]]></dc:creator>
		<pubDate>Mon, 12 Dec 2011 14:29:36 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-216088</guid>
		<description><![CDATA[i also have same problem. i too need the solution for same problem. using order by it is successful til 2nd level. please provide me the solution for same.]]></description>
		<content:encoded><![CDATA[<p>i also have same problem. i too need the solution for same problem. using order by it is successful til 2nd level. please provide me the solution for same.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jatin chawla</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-212516</link>
		<dc:creator><![CDATA[jatin chawla]]></dc:creator>
		<pubDate>Wed, 07 Dec 2011 04:45:09 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-212516</guid>
		<description><![CDATA[Very usefull thanks..]]></description>
		<content:encoded><![CDATA[<p>Very usefull thanks..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pooja</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-201228</link>
		<dc:creator><![CDATA[Pooja]]></dc:creator>
		<pubDate>Mon, 21 Nov 2011 11:58:31 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-201228</guid>
		<description><![CDATA[hey i want a solution. please help.
i have a table with these columns
ID GroupName ParentId
1   /                    0
2   a                   1
3   b                   2
4   a1                 1
5   b2                 4

and I want a output like

Groupname
/
/a
/a/b
/a1
/a1/b1

please help me asap.]]></description>
		<content:encoded><![CDATA[<p>hey i want a solution. please help.<br />
i have a table with these columns<br />
ID GroupName ParentId<br />
1   /                    0<br />
2   a                   1<br />
3   b                   2<br />
4   a1                 1<br />
5   b2                 4</p>
<p>and I want a output like</p>
<p>Groupname<br />
/<br />
/a<br />
/a/b<br />
/a1<br />
/a1/b1</p>
<p>please help me asap.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ram</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-186733</link>
		<dc:creator><![CDATA[ram]]></dc:creator>
		<pubDate>Wed, 02 Nov 2011 03:40:11 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-186733</guid>
		<description><![CDATA[hi Ravi/ Sathya
I am looking for the same output. can you please help me out with this query?]]></description>
		<content:encoded><![CDATA[<p>hi Ravi/ Sathya<br />
I am looking for the same output. can you please help me out with this query?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Abhishek</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-161917</link>
		<dc:creator><![CDATA[Abhishek]]></dc:creator>
		<pubDate>Thu, 25 Aug 2011 18:49:12 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-161917</guid>
		<description><![CDATA[I have a transaction table: Tran
 
Table Structure: Applid, SalesAmount, EmpId
 
I have an employee table: Employee
 
Table Structure: EmpId, EmpRole, ManagerId
 
Employee table can have 4 emp roles: A, B, C, D (D is the highest role, A is the lowest role)
 
The employee Id in Tran table (EmpId) can have employees with role A, B, C or D. Send me a query which would show the sum of SalesAmount for the employees at role D.
 
Tran Table:
Applid  SalesAmount  EmpID
1           1000              1001
2           2000              1002
3           3000              1003
4           4000              1004
5           5000              1005
6           6000              1006
7           7000              1007
8           8000              1008
9           9000              1009
10         10000            1001
 
Employee:
EmpID        Role       ManagerId
1001           A           1002
1002           B           1003
1003           C           1005
1004           A           1002
1005           D           ----
1006           A           1007
1007           B           1010
1008           B           1010
1010           C           1009
1009           D            ----
 
 
Desired output:
EmployeeID     TotalSalesAmount
1005                  25000
1009                  30000]]></description>
		<content:encoded><![CDATA[<p>I have a transaction table: Tran</p>
<p>Table Structure: Applid, SalesAmount, EmpId</p>
<p>I have an employee table: Employee</p>
<p>Table Structure: EmpId, EmpRole, ManagerId</p>
<p>Employee table can have 4 emp roles: A, B, C, D (D is the highest role, A is the lowest role)</p>
<p>The employee Id in Tran table (EmpId) can have employees with role A, B, C or D. Send me a query which would show the sum of SalesAmount for the employees at role D.</p>
<p>Tran Table:<br />
Applid  SalesAmount  EmpID<br />
1           1000              1001<br />
2           2000              1002<br />
3           3000              1003<br />
4           4000              1004<br />
5           5000              1005<br />
6           6000              1006<br />
7           7000              1007<br />
8           8000              1008<br />
9           9000              1009<br />
10         10000            1001</p>
<p>Employee:<br />
EmpID        Role       ManagerId<br />
1001           A           1002<br />
1002           B           1003<br />
1003           C           1005<br />
1004           A           1002<br />
1005           D           &#8212;-<br />
1006           A           1007<br />
1007           B           1010<br />
1008           B           1010<br />
1010           C           1009<br />
1009           D            &#8212;-</p>
<p>Desired output:<br />
EmployeeID     TotalSalesAmount<br />
1005                  25000<br />
1009                  30000</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gautam Kansal</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-151981</link>
		<dc:creator><![CDATA[Gautam Kansal]]></dc:creator>
		<pubDate>Sun, 31 Jul 2011 08:07:53 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-151981</guid>
		<description><![CDATA[Hi,

I have a table with below data
Column A             Column B
1                          101
1                          102
2                          103
2                          104
3                          105
3                          106

I need a result set as follows

101, 103, 105
101, 103, 106
101, 104, 105
101, 104, 106
102, 103, 105
102, 103, 106
102, 104, 105
102, 104, 106

Can you please help me how to get the above result using CTE]]></description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I have a table with below data<br />
Column A             Column B<br />
1                          101<br />
1                          102<br />
2                          103<br />
2                          104<br />
3                          105<br />
3                          106</p>
<p>I need a result set as follows</p>
<p>101, 103, 105<br />
101, 103, 106<br />
101, 104, 105<br />
101, 104, 106<br />
102, 103, 105<br />
102, 103, 106<br />
102, 104, 105<br />
102, 104, 106</p>
<p>Can you please help me how to get the above result using CTE</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Will</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-151357</link>
		<dc:creator><![CDATA[Will]]></dc:creator>
		<pubDate>Thu, 28 Jul 2011 18:06:53 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-151357</guid>
		<description><![CDATA[The CTE works well, but doesn&#039;t give the order I need. The problem I have is that I want to order the records as follows:

CEO
   Bob - Reports to CEO
      Joe - Reports to Bob
         Mike - Reports to Joe
      May - Reports to Bob
   Andy - Reports to CEO
      Tom - Reports to Andy

The spaces are for clarity

This is the code I am using at the moment:

    WITH GroupReports (GroupID, ReportsTo, GroupName, MemberID, Level)
AS
(
-- Anchor member definition
    SELECT e.GroupID, e.ReportsTo, e.GroupName, e.MemberID, 
        0 AS Level
    FROM dbo.Groups AS e
    WHERE e.ReportsTo IS NULL AND e.MemberID = @GroupID
    UNION ALL
-- Recursive member definition
    SELECT e.GroupID, e.ReportsTo, e.GroupName, e.MemberID, 
        Level + 1
    FROM Groups e
    INNER JOIN GroupReports d
        --ON e.GroupID = d.ReportsTo
        --ON e.ReportsTo = d.ReportsTo
        ON e.ReportsTo = d.GroupID
)
-- Statement that executes the CTE
SELECT gr.GroupID, gr.ReportsTo, gr.GroupName, gr.MemberID, Level
FROM GroupReports gr

And this is how it returns the rows:

CEO
   Bob - Reports to CEO
   Andy - Reports to CEO
     May - Reports to Bob
     Joe - Reports to Bob
   Tom - Reports to Andy
         Mike - Reports to Joe

It looks like all level one are selected, then all level two&#039;s and so on. That is not what I want. I need to have them grouped by &#039;Reports To&#039;.

Any help on this one would be much appreciated.]]></description>
		<content:encoded><![CDATA[<p>The CTE works well, but doesn&#8217;t give the order I need. The problem I have is that I want to order the records as follows:</p>
<p>CEO<br />
   Bob &#8211; Reports to CEO<br />
      Joe &#8211; Reports to Bob<br />
         Mike &#8211; Reports to Joe<br />
      May &#8211; Reports to Bob<br />
   Andy &#8211; Reports to CEO<br />
      Tom &#8211; Reports to Andy</p>
<p>The spaces are for clarity</p>
<p>This is the code I am using at the moment:</p>
<p>    WITH GroupReports (GroupID, ReportsTo, GroupName, MemberID, Level)<br />
AS<br />
(<br />
&#8211; Anchor member definition<br />
    SELECT e.GroupID, e.ReportsTo, e.GroupName, e.MemberID,<br />
        0 AS Level<br />
    FROM dbo.Groups AS e<br />
    WHERE e.ReportsTo IS NULL AND e.MemberID = @GroupID<br />
    UNION ALL<br />
&#8211; Recursive member definition<br />
    SELECT e.GroupID, e.ReportsTo, e.GroupName, e.MemberID,<br />
        Level + 1<br />
    FROM Groups e<br />
    INNER JOIN GroupReports d<br />
        &#8211;ON e.GroupID = d.ReportsTo<br />
        &#8211;ON e.ReportsTo = d.ReportsTo<br />
        ON e.ReportsTo = d.GroupID<br />
)<br />
&#8211; Statement that executes the CTE<br />
SELECT gr.GroupID, gr.ReportsTo, gr.GroupName, gr.MemberID, Level<br />
FROM GroupReports gr</p>
<p>And this is how it returns the rows:</p>
<p>CEO<br />
   Bob &#8211; Reports to CEO<br />
   Andy &#8211; Reports to CEO<br />
     May &#8211; Reports to Bob<br />
     Joe &#8211; Reports to Bob<br />
   Tom &#8211; Reports to Andy<br />
         Mike &#8211; Reports to Joe</p>
<p>It looks like all level one are selected, then all level two&#8217;s and so on. That is not what I want. I need to have them grouped by &#8216;Reports To&#8217;.</p>
<p>Any help on this one would be much appreciated.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roshni</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-149297</link>
		<dc:creator><![CDATA[Roshni]]></dc:creator>
		<pubDate>Tue, 19 Jul 2011 07:25:25 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-149297</guid>
		<description><![CDATA[Hello Pinal,

My Table Contains Such Type of Record
ID    Name       ParentID
1     Item 1       NULL
2     Item 2       NULL
3     Cate 1      1
4     Cate 2      2
5     Cate 3      1
6     Cate 4      1
7     Cate 5      1
8     Cate 6      2
...... so on ....  may be there is possibility of child of Cate 1, Cate 2....

Actually using CTE, It will Give me Output Like this :
ID    Name       ParentID
1     Item 1       NULL
2     Item 2       NULL
4     Cate 2      2
8     Cate 6      2
3     Cate 1      1
5     Cate 3      1
6     Cate 4      1
7     Cate 5      1

But I want OutPut Like this :
ID    Name       ParentID
1     Item 1       NULL
3     Cate 1      1
5     Cate 3      1
6     Cate 4      1
7     Cate 5      1
2     Item 2       NULL
4     Cate 2      2
8     Cate 6      2

How can I get Such Type of Output ??.... Please Help me ...]]></description>
		<content:encoded><![CDATA[<p>Hello Pinal,</p>
<p>My Table Contains Such Type of Record<br />
ID    Name       ParentID<br />
1     Item 1       NULL<br />
2     Item 2       NULL<br />
3     Cate 1      1<br />
4     Cate 2      2<br />
5     Cate 3      1<br />
6     Cate 4      1<br />
7     Cate 5      1<br />
8     Cate 6      2<br />
&#8230;&#8230; so on &#8230;.  may be there is possibility of child of Cate 1, Cate 2&#8230;.</p>
<p>Actually using CTE, It will Give me Output Like this :<br />
ID    Name       ParentID<br />
1     Item 1       NULL<br />
2     Item 2       NULL<br />
4     Cate 2      2<br />
8     Cate 6      2<br />
3     Cate 1      1<br />
5     Cate 3      1<br />
6     Cate 4      1<br />
7     Cate 5      1</p>
<p>But I want OutPut Like this :<br />
ID    Name       ParentID<br />
1     Item 1       NULL<br />
3     Cate 1      1<br />
5     Cate 3      1<br />
6     Cate 4      1<br />
7     Cate 5      1<br />
2     Item 2       NULL<br />
4     Cate 2      2<br />
8     Cate 6      2</p>
<p>How can I get Such Type of Output ??&#8230;. Please Help me &#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER – Interview Questions and Answers – Frequently Asked Questions – Day 15 of 31 Journey to SQLAuthority</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-148374</link>
		<dc:creator><![CDATA[SQL SERVER – Interview Questions and Answers – Frequently Asked Questions – Day 15 of 31 Journey to SQLAuthority]]></dc:creator>
		<pubDate>Fri, 15 Jul 2011 01:31:29 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-148374</guid>
		<description><![CDATA[[...] CTE is the abbreviation for Common Table Expression. A CTE is an expression that can be thought of as a temporary result set which is defined within the execution of a single SQL statement. A CTE is similar to a derived table in that it is not stored as an object and lasts only for the duration of the query. (Read more here) [...]]]></description>
		<content:encoded><![CDATA[<p>[...] CTE is the abbreviation for Common Table Expression. A CTE is an expression that can be thought of as a temporary result set which is defined within the execution of a single SQL statement. A CTE is similar to a derived table in that it is not stored as an object and lasts only for the duration of the query. (Read more here) [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ilan</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-144196</link>
		<dc:creator><![CDATA[Ilan]]></dc:creator>
		<pubDate>Mon, 27 Jun 2011 14:02:21 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-144196</guid>
		<description><![CDATA[is there a way to avoid a loop (nocycle for those of you who know oracle plsql) using cte?]]></description>
		<content:encoded><![CDATA[<p>is there a way to avoid a loop (nocycle for those of you who know oracle plsql) using cte?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ravi</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-137190</link>
		<dc:creator><![CDATA[ravi]]></dc:creator>
		<pubDate>Sat, 28 May 2011 13:03:04 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-137190</guid>
		<description><![CDATA[hi satya,

i&#039;m trying this code.but it shows an error i think it not works .will u check it and send me clearly



thanks,
ravi]]></description>
		<content:encoded><![CDATA[<p>hi satya,</p>
<p>i&#8217;m trying this code.but it shows an error i think it not works .will u check it and send me clearly</p>
<p>thanks,<br />
ravi</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tynicool</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-137091</link>
		<dc:creator><![CDATA[tynicool]]></dc:creator>
		<pubDate>Fri, 27 May 2011 22:25:06 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-137091</guid>
		<description><![CDATA[Sorry for my english,

How can I use CTE in SSAS ,creating a named query  on a DSV?]]></description>
		<content:encoded><![CDATA[<p>Sorry for my english,</p>
<p>How can I use CTE in SSAS ,creating a named query  on a DSV?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: satish</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-135192</link>
		<dc:creator><![CDATA[satish]]></dc:creator>
		<pubDate>Thu, 19 May 2011 06:20:44 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-135192</guid>
		<description><![CDATA[Thank you so much, It is very useful.]]></description>
		<content:encoded><![CDATA[<p>Thank you so much, It is very useful.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Manish</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-134963</link>
		<dc:creator><![CDATA[Manish]]></dc:creator>
		<pubDate>Wed, 18 May 2011 07:51:25 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-134963</guid>
		<description><![CDATA[Hi all,

I am having a temptable with 100 names in it, and I want to execute a SP with all the 100 names as the parameters of the temptable and the output of the SP I want to store in another temp table say(XtempTable). 

Can I achieve this using CTE. if yes..
Please advice how can I go with that using CTE.

Thanks
Manish]]></description>
		<content:encoded><![CDATA[<p>Hi all,</p>
<p>I am having a temptable with 100 names in it, and I want to execute a SP with all the 100 names as the parameters of the temptable and the output of the SP I want to store in another temp table say(XtempTable). </p>
<p>Can I achieve this using CTE. if yes..<br />
Please advice how can I go with that using CTE.</p>
<p>Thanks<br />
Manish</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER – Common Table Expression (CTE) and Few Observation Journey to SQLAuthority</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-133694</link>
		<dc:creator><![CDATA[SQL SERVER – Common Table Expression (CTE) and Few Observation Journey to SQLAuthority]]></dc:creator>
		<pubDate>Tue, 10 May 2011 02:16:59 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-133694</guid>
		<description><![CDATA[[...] SQL SERVER – Simple Example of Recursive CTE [...]]]></description>
		<content:encoded><![CDATA[<p>[...] SQL SERVER – Simple Example of Recursive CTE [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jx</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-115371</link>
		<dc:creator><![CDATA[jx]]></dc:creator>
		<pubDate>Mon, 31 Jan 2011 13:26:37 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-115371</guid>
		<description><![CDATA[would it be possible to query the managers for 
9 Dodsworth 5?
e.g. it would return 
2 Fuller        Null
5 Buchanan 2

because Buchanan is his direct superier, and Fuller is Buchanans superior?]]></description>
		<content:encoded><![CDATA[<p>would it be possible to query the managers for<br />
9 Dodsworth 5?<br />
e.g. it would return<br />
2 Fuller        Null<br />
5 Buchanan 2</p>
<p>because Buchanan is his direct superier, and Fuller is Buchanans superior?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jx</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-115369</link>
		<dc:creator><![CDATA[jx]]></dc:creator>
		<pubDate>Mon, 31 Jan 2011 13:25:04 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-115369</guid>
		<description><![CDATA[Thanks for the link,
THe resulting dataset is 
2	Fuller		NULL
1	Davolio		2
3	Leverling	2
4	Peacock		2
5	Buchanan	2
8	Callahan	2
6	Suyama		5
7	King		5
9	Dodsworth	5]]></description>
		<content:encoded><![CDATA[<p>Thanks for the link,<br />
THe resulting dataset is<br />
2	Fuller		NULL<br />
1	Davolio		2<br />
3	Leverling	2<br />
4	Peacock		2<br />
5	Buchanan	2<br />
8	Callahan	2<br />
6	Suyama		5<br />
7	King		5<br />
9	Dodsworth	5</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard Hansell</title>
		<link>http://blog.sqlauthority.com/2008/07/28/sql-server-simple-example-of-recursive-cte/#comment-113873</link>
		<dc:creator><![CDATA[Richard Hansell]]></dc:creator>
		<pubDate>Mon, 24 Jan 2011 16:25:29 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=748#comment-113873</guid>
		<description><![CDATA[Yes, this query is pretty useless in this form.  All it does is list out the contents of the table in the &quot;right&quot; order.  However, we can easily &quot;fix&quot; it to make it much more useful:

WITH Emp_CTE AS (
SELECT EmployeeID, ManagerID, Title, 0 AS Depth
FROM HumanResources.Employee
WHERE ManagerID IS NULL
UNION ALL
SELECT e.EmployeeID, e.ManagerID, e.Title, ecte.Depth + 1 AS Depth
FROM HumanResources.Employee e
INNER JOIN Emp_CTE ecte ON ecte.EmployeeID = e.ManagerID
)
SELECT *
FROM Emp_CTE

All I am doing is adding a little contextual information to the query and removing some of the junk we don&#039;t really need.  I hope the syntax is right as I don&#039;t have a copy of the old AdventureWorks database to test it!]]></description>
		<content:encoded><![CDATA[<p>Yes, this query is pretty useless in this form.  All it does is list out the contents of the table in the &#8220;right&#8221; order.  However, we can easily &#8220;fix&#8221; it to make it much more useful:</p>
<p>WITH Emp_CTE AS (<br />
SELECT EmployeeID, ManagerID, Title, 0 AS Depth<br />
FROM HumanResources.Employee<br />
WHERE ManagerID IS NULL<br />
UNION ALL<br />
SELECT e.EmployeeID, e.ManagerID, e.Title, ecte.Depth + 1 AS Depth<br />
FROM HumanResources.Employee e<br />
INNER JOIN Emp_CTE ecte ON ecte.EmployeeID = e.ManagerID<br />
)<br />
SELECT *<br />
FROM Emp_CTE</p>
<p>All I am doing is adding a little contextual information to the query and removing some of the junk we don&#8217;t really need.  I hope the syntax is right as I don&#8217;t have a copy of the old AdventureWorks database to test it!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

