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 > Recursive Query or Procedure

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-03-07, 22:25
asalamay asalamay is offline
Registered User
 
Join Date: Mar 2005
Posts: 22
Recursive Query or Procedure

Hi I'm working on building a recursive query or procedure. I have a table that list the employees for the company. each row of data contains the employee ID and the Managers ID. I was making a query that did 11 joins from the Employee ID to the Managers ID. The hiearchy of the data is current 11 joins deep, but this could change and I would like to know if there is a dynamic way to create the joins the return the data. So I don't have to count how many joins are needed.

So what is the best way to handle this and if you post explain how the solution works.
Reply With Quote
  #2 (permalink)  
Old 10-04-07, 03:46
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Do you really need to show 11 levels of heirachy?

Personally I'd go with 3:
An employee, their manager(s) and the people who report into the employee.
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 10-04-07, 07:00
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You can simply use recursive queries:
Code:
WITH RECURSIVE hierarchy
   AS ( SELECT ...
        FROM   ...
        WHERE  ...
        UNION ALL
        SELECT ...
        FROM   ..., hierarchy
        WHERE  ... )
SELECT ...
FROM   hierarchy
WHERE  ...
Since SQL is a relational programming language, I wouldn't use stored procedures for such a task.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #4 (permalink)  
Old 10-04-07, 07:01
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
what kind of paleolithic organization has that many levels?

george's suggestion is one of the best for this type of problem

another is simply to code 17 levels of joins and give yourself some leeway

(and no, there is no difficulty in the database joining that many tables)

if neither of these suggestions satisfies, then you may wish to change your data model from the adjacency model (which is what the id/parentid model is called) to the nested set model
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 10-13-07, 15:01
sco08y sco08y is offline
Registered User
 
Join Date: Oct 2002
Location: Baghdad, Iraq
Posts: 697
Quote:
Originally Posted by r937
what kind of paleolithic organization has that many levels?
Government and military. Hell, military has a *dual* hierarchy.
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