cyborg007
cyborg007

Reputation: 123

Recurring Selection of rows based on value of the value in previous row

I have the typical emp table from which i want to fetch the hierarchical organisation chart of the employee specified.

i.e. if I Enter 101 as the EMPID in where clause then it should show:

ID  NAME   MGRID
101 SCOTT   104
104 TIGER   106
106 KING    ---

I want to know if it can be solved with SQL or I have to use PL/SQL ? Thanks.

Upvotes: 1

Views: 731

Answers (2)

Dojo
Dojo

Reputation: 5684

Assuming you are using oracle, I think Hierarchical Queries are just what you need:

http://docs.oracle.com/cd/B19306_01/server.102/b14200/queries003.htm

Upvotes: 1

user330315
user330315

Reputation:

SELECT id, name, mgrid
FROM emp
START WITH id = 101
CONNECT BY PRIOR mgrid = id
ORDER BY id

Upvotes: 2

Related Questions