If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > construct hierarchy

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-04-05, 03:17
sashish4529 sashish4529 is offline
Registered User
 
Join Date: Feb 2005
Posts: 29
construct hierarchy

I have an emp table with the two columns
emp_no and mgr_no

say for e.g
emp_no mgr_no
--------------------
10 100
15 90
20 100
25 150
30 100
90 200
100 200
150 200
200 300
300 400

I want to construct a query for empno = 100, which will construct hieachy both above and one level below.... e.g
400
300
200
100
30
20
10

Here 10,20, and 30 are directly below Mgr 100

For emp_no 200 the output should be
400
300
200
150
100
90
Here emp - 100,90 and 150 report to Mgr - 200


Any suggestions/comments ?

Many Thanks.

Ash
Reply With Quote
  #2 (permalink)  
Old 03-04-05, 03:39
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Code:
select emp_no
from emp
where mgr_no = &&par_emp_no
union
select mgr_no 
from emp
where mgr_no >= &&par_emp_no
order by 1 desc;
"&&", in Oracle, requires you to insert value for a parameter "par_emp_no". If you use another DB, see if it needs to be changed.
Also, I'd say that your first example lacks in mgr_no = 150 (which is higher than the parametrized 100).
Reply With Quote
  #3 (permalink)  
Old 03-04-05, 03:56
sashish4529 sashish4529 is offline
Registered User
 
Join Date: Feb 2005
Posts: 29
Thanks for the suggestion. This works as per the example I had given.

However it is not necesssary that the manager's empno is greater than his employee.
Reply With Quote
  #4 (permalink)  
Old 03-04-05, 11:33
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
ash, if you go all the way up the tree from any given level, what is the maximum dpth of the tree?

also, if you just dump them out in one column, like this --

400
300
200
150
100
90

what possible use could this be? how do you know which one's the boss, which one's the boss's boss, which one's the subordinate?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Last edited by r937; 03-04-05 at 13:25.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On