niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Databases » Oracle
multilevel hierarchy query
Post new topic   Reply to topic Page 1 of 1 [8 Posts] View previous topic :: View next topic
Author Message
bruce@aristotle.net
*nix forums beginner


Joined: 17 Feb 2005
Posts: 1

PostPosted: Thu Feb 17, 2005 5:23 pm    Post subject: Re: multilevel hierarchy query Reply with quote

If you _know_ the number of levels of the hierarchy in advance, then
this is probably the way to go. Otherwise you're probably going to
need to resort to a recursive VBA code solution of some kind. It would
be an intriguing exercise to combine the two, i.e., write code to
determine the actual depth of the hierarchy based on your actual data
and then actually generate the SQL from code...

Bruce
Back to top
baphensley
*nix forums beginner


Joined: 15 Feb 2005
Posts: 3

PostPosted: Wed Feb 16, 2005 5:17 pm    Post subject: Re: multilevel hierarchy query Reply with quote

David,

Thanks. That was an interesting read.

Unfortunately, I've inherited the tables and can't change them, just
read them.

However, I can estimate the maximum number of levels in the tree. With
this in mind, I tried a brute force approach. This seems to get all
the staff below a manager (as long as they're no more than 6 levels
deep). Not elegant, but it seems to be effective.

SELECT tblStaff.StaffID, [tblStaff]![StaffID] & " " &
[tblStaff]![ReportsToID] & " " & [B2]![ReportsToID] & " " &
[B3]![ReportsToID] & " " & [B4]![ReportsToID] & " " &
[B5]![ReportsToID] AS ChainOCmd
FROM ((((tblStaff LEFT JOIN tblStaff AS B1 ON tblStaff.ReportsToID =
B1.StaffID) LEFT JOIN tblStaff AS B2 ON B1.ReportsToID = B2.StaffID)
LEFT JOIN tblStaff AS B3 ON B2.ReportsToID = B3.StaffID) LEFT JOIN
tblStaff AS B4 ON B3.ReportsToID = B4.StaffID) LEFT JOIN tblStaff AS B5
ON B4.ReportsToID = B5.StaffID
WHERE ((([tblStaff]![StaffID] & " " & [tblStaff]![ReportsToID] & " " &
[B2]![ReportsToID] & " " & [B3]![ReportsToID] & " " &
[B4]![ReportsToID] & " " & [B5]![ReportsToID]) Like "*" & [Boss: ] &
"*"));


Thanks,
Bruce
Back to top
David Schofield
*nix forums beginner


Joined: 16 Feb 2005
Posts: 1

PostPosted: Wed Feb 16, 2005 8:38 am    Post subject: Re: multilevel hierarchy query Reply with quote

Hi
If you are into hierarchies, take a look at "Trees in SQL" by Joe
Celko

http://www.intelligententerprise.com/001020/celko.jhtml?_requestid=29530

David
Back to top
baphensley
*nix forums beginner


Joined: 15 Feb 2005
Posts: 3

PostPosted: Tue Feb 15, 2005 8:37 pm    Post subject: Re: multilevel hierarchy query Reply with quote

Greg,

Thanks for the tip. However, I can't seem to find anything in the
Access 97 documentation on the SHAPE command. I think it may have been
introduced with Access 2000.

Bruce
Back to top
baphensley
*nix forums beginner


Joined: 15 Feb 2005
Posts: 3

PostPosted: Tue Feb 15, 2005 6:25 pm    Post subject: Re: multilevel hierarchy query Reply with quote

Rauf,

Thanks! Excellent!!

This is perfect for my Oracle data.

Now I need to do the same in Access 97 tables. I can't find anything
similar
to CONNECT BY for Access 97.

Bruce

Rauf Sarwar wrote:
Quote:
Bruce Hensley wrote:
I don't know how to begin on a query (SELECT statement) to find all
the
tasks assigned to an arbitrary manager (say, staffID='JSmith') and
her
organization, that is, assigned to all her underlings, and their
underlings,
and .... For that matter, I don't even know how to find everyone
in
her
organization (at all levels).

- All individuals have only one manager
- Tasks are assigned to individuals
- A manager at any level may have direct reports and sub-managers

The table structure:

tblStaff
-------------
staffID
reportsToID (staffID of direct manager)


tblTasks
--------------
taskID
assignedToID (staffID of individual responsible for task)


Any help would be greatly appreciated.

Bruce

You can find information about hierarchical queries at

http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96540/queries4a.htm#2053937

URL may wrap.

Regards
/Rauf
Back to top
Greg Teets
*nix forums beginner


Joined: 15 Feb 2005
Posts: 1

PostPosted: Tue Feb 15, 2005 6:00 pm    Post subject: Re: multilevel hierarchy query Reply with quote

On 15 Feb 2005 10:17:15 -0800, "Rauf Sarwar" <rs_arwar@hotmail.com>
wrote:

Quote:

Bruce Hensley wrote:
I don't know how to begin on a query (SELECT statement) to find all
the
tasks assigned to an arbitrary manager (say, staffID='JSmith') and
her
organization, that is, assigned to all her underlings, and their
underlings,
and .... For that matter, I don't even know how to find everyone in
her
organization (at all levels).

- All individuals have only one manager
- Tasks are assigned to individuals
- A manager at any level may have direct reports and sub-managers

The table structure:

tblStaff
-------------
staffID
reportsToID (staffID of direct manager)


tblTasks
--------------
taskID
assignedToID (staffID of individual responsible for task)


Any help would be greatly appreciated.

Bruce

I haven't used them yet but you may find the Shaped Query for ADO

helpful.

Just do a search in Google.

Good luck.
Greg Teets
Cincinnati Ohio USA
Back to top
Rauf Sarwar
*nix forums Guru


Joined: 03 May 2005
Posts: 353

PostPosted: Tue Feb 15, 2005 5:17 pm    Post subject: Re: multilevel hierarchy query Reply with quote

Bruce Hensley wrote:
Quote:
I don't know how to begin on a query (SELECT statement) to find all
the
tasks assigned to an arbitrary manager (say, staffID='JSmith') and
her
organization, that is, assigned to all her underlings, and their
underlings,
and .... For that matter, I don't even know how to find everyone in
her
organization (at all levels).

- All individuals have only one manager
- Tasks are assigned to individuals
- A manager at any level may have direct reports and sub-managers

The table structure:

tblStaff
-------------
staffID
reportsToID (staffID of direct manager)


tblTasks
--------------
taskID
assignedToID (staffID of individual responsible for task)


Any help would be greatly appreciated.

Bruce

You can find information about hierarchical queries at
http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96540/queries4a.htm#2053937

URL may wrap.

Regards
/Rauf
Back to top
Vincent M
*nix forums Guru


Joined: 22 Feb 2005
Posts: 1332

PostPosted: Tue Feb 15, 2005 4:56 pm    Post subject: multilevel hierarchy query Reply with quote

I don't know how to begin on a query (SELECT statement) to find all the
tasks assigned to an arbitrary manager (say, staffID='JSmith') and her
organization, that is, assigned to all her underlings, and their underlings,
and .... For that matter, I don't even know how to find everyone in her
organization (at all levels).

- All individuals have only one manager
- Tasks are assigned to individuals
- A manager at any level may have direct reports and sub-managers

The table structure:

tblStaff
-------------
staffID
reportsToID (staffID of direct manager)


tblTasks
--------------
taskID
assignedToID (staffID of individual responsible for task)


Any help would be greatly appreciated.

Bruce
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [8 Posts] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 2:41 am | All times are GMT
navigation Forum index » Databases » Oracle
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Very slow query Michael Sutter MySQL 0 Fri Jul 21, 2006 1:10 pm
No new posts Shortening URLs passing through a squid hierarchy Irvine, Doug - Resources Squid 0 Fri Jul 21, 2006 10:15 am
No new posts Column info without executing query Dan Strömberg PostgreSQL 10 Thu Jul 20, 2006 6:21 pm
No new posts recursive query Jürg Schaufelberger Server 1 Thu Jul 20, 2006 6:06 pm
No new posts How to query on part of a date column? Barry Newton MySQL 4 Thu Jul 20, 2006 5:02 pm

Credit Card | Debt Consolidation | Debt Consolidation | Western Union Locations | Debt Consolidation
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.3324s ][ Queries: 20 (0.1544s) ][ GZIP on - Debug on ]