Solved

recursive query

Posted on 2011-03-09
4
214 Views
Last Modified: 2012-05-11
How does one write a recursive query?  This would come into play where a table is self-referencing.   Like, a list of employees, where there is a primary Key and supervisor column, where the latter maps to the primary key.   So, If I want a list of employees everyone from person "X" on down, how do I do this?
0
Comment
Question by:HLRosenberger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35087033

You can use a recursive Common Table Expression (CTE) to do this kind of query
0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 500 total points
ID: 35087074
here is an example

ID Parent
1  null
2  1
3  1
4  null
5  4
6  5

with CTE(ID, PARENT)
AS
(
      SELECT F.ID, F.PARENT
      FROM TABLE1 F
      WHERE PARENT IS NULL
      UNION ALL
      SELECT F.ID, F.PARENT
      FROM TABLE1 F
      INNER JOIN CTE FH ON FH.ID = F.PARENT
)

SELECT ID, PARENT FROM CTE
0
 
LVL 9

Expert Comment

by:gdupadhyay
ID: 35087209
I believe you are looking to make a tree for employee:
See example below:
ID: Emp ID
Name: Emp Name
Manager: Emp. Manager ID (NULL: Emp. have no manager)

ID      Name      Manager
1      a      NULL
2      b      1
3      c      1
4      X      2
5      Y      2
6      Z      1
7      P      3

WITH Employee_Tree (ID,
		Name,
		Manager,
		Depth) AS(
    SELECT  ID,
		Name,
		Manager,
		1 AS depth
     FROM Employee WITH (NOLOCK)
     WHERE MANAGER IS NULL
   UNION ALL
	SELECT  N.ID,
	        N.Name,
		N.Manager,
		ET.Depth + 1
		FROM Employee AS N WITH (NOLOCK)
	INNER JOIN Employee_Tree ET 
		ON ET.ID = N.Manager)
		SELECT ID,
		   Name,
		   Manager,
	FROM Employee_Tree

Open in new window

0
 
LVL 1

Author Closing Comment

by:HLRosenberger
ID: 35124508
Thanks!
0

Featured Post

PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question