Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 1st, 2009, 01:33 AM
Bobby123 Bobby123 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 82 Bobby123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 17 m 19 sec
Reputation Power: 6
How to Display a Hierarchy List?

I'm looking for the easiest query in order to display a hierarchy list. Example:

Database has three tables

Code:
Heading     --> id, status, order, title
Sub-heading --> id, status, order, title, heading_id
Item        --> id, status, order, title, heading_id, sub-heading_id, actual_content

order = the sequence in which they are to be displayed
a sub-heading's heading_id refers to the parent heading, likewise
an item's sub-heading_id refers to the parent sub-heading.

I assumed there was no need to track child content. 


Now, the output format should be something like this, where the sub-headings are not repeated, nor are the items. Sort of like chapters in a book, things in a catalogue, or even topics in a forum.

Code:
Heading 1
    Sub-heading 1
        Item 1
        Item 2
        Item 3
    Sub-heading 2
        Item 1
        Item 2
        Item 3
Heading 2
    Sub-heading 1
        Item 1
        Item 2
        Item 3
Heading 3
    Sub-heading 1
        Item 1
        Item 2
        Item 3
    Sub-heading 2
        Item 1
        Item 2
        Item 3
    Sub-heading 3
        Item 1
        Item 2
        Item 3  


Where I'm getting hung up is how to query the data so it's quick, efficient, clean and easy to read and maintain. I've only gotten to listing the sub-headings and already the query looks convoluted. So, how should I approach this? Should it be done all in one query, and always checking against the results to know when to print a new heading or sub heading. Or should I run multiple queries and save the results to a variable, and then run sub-queries off that? Is there a better way to do this, am I going about it the hard way or am I in the right direction and just haven't gone far enough with it yet?

Thanks for your insights.

Reply With Quote
  #2  
Old October 1st, 2009, 07:35 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 20,766 r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 1 Week 3 Days 17 h 29 m 26 sec
Reputation Power: 2481
your schema can be simplified by combining the heading and subheading tables as described here --Categories and Subcategories

the query should be a single, simple query

formatting the results into a nicely indented list is the job of your application language
Comments on this post
Bobby123 agrees: This is exactly what I needed.
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old October 1st, 2009, 09:57 AM
Bobby123 Bobby123 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 82 Bobby123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 17 m 19 sec
Reputation Power: 6
Thumbs up

Thanks, Rudy. That should get me where I'm going. The resource links are pretty good too.


PS. I just ordered your book.


Cheers, and thanks again.

Reply With Quote
  #4  
Old October 1st, 2009, 10:53 AM
mateoc15's Avatar
mateoc15 mateoc15 is offline
C A R D S
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2004
Location: The 'Ville
Posts: 890 mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 22 h 58 m 41 sec
Reputation Power: 14
I have a similar issue I think. If I need to create a new post let me know.

The situation I have is there are two basic tables.

1. User table - contains a user ID and a position ID
2. Positions - contains a position ID and parent position ID (who the position reports to)

I want to display the full hierarchy which could be any number of levels deep. A parent could have a child, which could have 3 children, each of which could have 2 children, and one of those has a child, etc. Any number of levels.

I want to create some logic to print these and indent at each level. I realize the indenting can't be done using a simple SQL query but I want to get the correct information to use a front end (ASP.NET or whatever) to do this.

Does this require the use of cursors? If so, how can I tell how many levels deep I need to go (ie - how many nested cursors to use, which I think will be a bad idea)?

For ASP.NET'ers maybe there's a good way to bind this to a TreeView control or something?

Right now I really just need the logic for gathering the data.

NOTE: the schema cannot be changed.

Thanks!
__________________
SELECT COUNT(*) FROM fun WHERE location = 'WORK' AND day_of_week IN ('MON','TUE','WED','THU','FRI');

COUNT
-------
0

Last edited by mateoc15 : October 1st, 2009 at 11:01 AM.

Reply With Quote
  #5  
Old October 1st, 2009, 08:06 PM
Bobby123 Bobby123 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 82 Bobby123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 17 m 19 sec
Reputation Power: 6
I think you'll find most of your answers in the link r937 provided. It uses an example very much like you're describing. It uses a single table, but you can join your two tables using the same method. You can use almost any language to query then output the data - asp and php are both fine, I use perl myself. To produce the indented format, I think an unordered list <ul> should do the trick.

Reply With Quote
  #6  
Old October 1st, 2009, 09:55 PM
mateoc15's Avatar
mateoc15 mateoc15 is offline
C A R D S
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2004
Location: The 'Ville
Posts: 890 mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 22 h 58 m 41 sec
Reputation Power: 14
That example is helpful but it uses the down1, down2, down3, etc so you know that you can have a root and three levels below that, but I don't know how many levels I'll have. It could be 100. How can I adapt the example to fit for me?

Reply With Quote
  #7  
Old October 1st, 2009, 10:21 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 20,766 r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 1 Week 3 Days 17 h 29 m 26 sec
Reputation Power: 2481
Quote:
Originally Posted by mateoc15
It could be 100.
nonsense

"Positions - contains a position ID and parent position ID (who the position reports to)"

unless it is the federal government, i don't think there's even a remote chance that you have 100 levels of bosses

Reply With Quote
  #8  
Old October 2nd, 2009, 02:31 AM
shammat shammat is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 1,557 shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 3 Days 22 h 43 m 21 sec
Reputation Power: 209
Quote:
Originally Posted by mateoc15
That example is helpful but it uses the down1, down2, down3, etc so you know that you can have a root and three levels below that, but I don't know how many levels I'll have. It could be 100. How can I adapt the example to fit for me?
Depends on your DBMS.
With Oracle use CONNECT BY to retrieve a hierarchical list.
With Postgres, Firebird or SQL Server use a recursive common table expression.
With other databases you are probably out of luck (the ones I listed are the only ones I know of to support recursive CTE)

Reply With Quote
  #9  
Old October 3rd, 2009, 06:39 PM
mateoc15's Avatar
mateoc15 mateoc15 is offline
C A R D S
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2004
Location: The 'Ville
Posts: 890 mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 22 h 58 m 41 sec
Reputation Power: 14
Quote:
Originally Posted by r937
nonsense

"Positions - contains a position ID and parent position ID (who the position reports to)"

unless it is the federal government, i don't think there's even a remote chance that you have 100 levels of bosses


Thanks for the worthless reply.

The idea is that there could be ANY number of levels. It should be dynamic! We do have some parts of the "tree" that are 20-25 levels deep. But that's not the point.

Reply With Quote
  #10  
Old October 3rd, 2009, 06:40 PM
mateoc15's Avatar
mateoc15 mateoc15 is offline
C A R D S
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2004
Location: The 'Ville
Posts: 890 mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 22 h 58 m 41 sec
Reputation Power: 14
Ok. I am using Oracle, so I guess the CONNECT BY would work. I do know that the CONNECT BY does require an index on the column. I'll have to see if that exists. Hopefully it does.

Thanks!

Reply With Quote
  #11  
Old October 7th, 2009, 07:53 PM
Bobby123 Bobby123 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 82 Bobby123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 17 m 19 sec
Reputation Power: 6
Posted using the topic reply button (page 1 = last page at this time)

Perhaps we might be a bit more productive here if we couch the question(s) in somewhat of a better context. So, for example, about ten years ago wwwboard from Matt's Script Archive was very popular. It used a threaded list and a ton of recursion to display pages and topics... to the point that displaying a popular topic got quite unruly with subject headings squished hard against the right side of the page. That, among many other factors, led to the development of in-line forum styles that we see today, including this one, which, fortunately, allows a threaded view. So, goto this, for example

http://forums.devshed.com/mysql-help-4/mysql-resources-136196.html

Then, at the top right of the page goto "Display Modes > Switch to Threaded Mode" to see a linked tree. Granted that is not exactly what mateoc15 is asking for but it puts us in an interesting direction. Even though forum posts are displayed 'in-line' so to speak, how a post is recorded depends on how you enter the topic (main link, recent post, or a specific page) and which button you click to reply (a poster's button, the topic button, quick reply), so that the 'tree' appears more varied than you'd think. Now, that particular topic only goes five or six levels deep. How does the forum program generate that tree? Surely, it can't anticipate the number of branches/leaves a thread might have. On another forum, I know of a topic that is over a hundred pages long. How would 'this' forum display a threaded view in that case, especially if the tree had a single branch? (Sorry, I looked for a topic with more posts but didn't find one).

The article linked above, which, by the way, was perfectly suited to my application, discourages query recursion but does not really go into the whys and wherefores of the matter. I'm guessing that in the situation I referred to about a topic with hundreds of pages and thousands of replies query recursion would result in a single query for every post, which probably isn't the best way to go.

That's probably the long way around to making a point. But the forum instance is analogical to what mateoc15 describes. A redesigned database is probably the best solution, but by no means is it the only one. The forum here proves that a dynamic system is possible. I'm just not the guy (yet) to say how to go about it.

Reply With Quote
  #12  
Old November 16th, 2009, 06:13 PM
Bobby123 Bobby123 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 82 Bobby123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 17 m 19 sec
Reputation Power: 6
[ update - since I can't edit my previous post ]

I found a very long thread here on devshed. Both links take you to the same place.

http://forums.devshed.com/showthread.php?postid=1404352#1404352
http://forums.devshed.com/the-outhouse-138/the-longest-thread-on-devsheds-327568.html

If you switch to threaded view, you can see the tree running off to the right side. Be warned, however, that trying to view the page for me, when set to threaded, prevented the page from displaying properly. I had to turn threaded view off in another thread, then go to the long one, and switch it back on. The point: a dynamic hierarchal tree is definitely possible, even if problematic.

Since my original post above, I've learned that you should probably run the fewest queries possible. That said, recursion, if that is the answer, is possible if you must. One day somebody should look at how vbulletin (or whatever this forum is) handles it.




Lastly, while I'm here, I do not agree this topic should have been moved to oracle. Just because a member decides his concerns are similar to the original post, does not mean the topic should have been hijacked into something it was never meant to be. This was a general sql question and that's where it was posted and where it should have remained. But so be it.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > How to Display a Hierarchy List?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek