Home > Ask the Oracle Database / Applications Experts > SQL Questions & Answers > Counting rows in a subtree
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Counting rows in a subtree

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 23 April 2004

There is a table called User (userID PK, userName, supUserID FK). SupUserID refers to userID and contains the userID value of the user's superior. The highest user has no user superior to itself (NULL instead of a value) and can have under itself one or more users or no users at all. Number of levels in such a tree is not defined.

Is there a way to count all users under the highest user by using SQL only (not Transact-SQL) or something else? Hope it's a solvable question in general.


>

In general, this requires recursion. Yours is the classic hierarchical structure, implemented using the adjacency model. As I mentioned in a previous answer, Recursion in SQL, only Oracle, to my knowledge, provides a way to do recursion.

Of course, if you can store your data using the nested set model, then the solution is straight-forward, I'm told (I don't actually use the nested set model myself). See Converting an adjacency list model to a nested set model.

If you must stick with the adjacency model, you can "work around" the need for recursion, provided you are able to set a realistic limit to the depth of the subtree. Just use an appropriate number of self-joins. If the path from the CEO to the lowliest cube jockey is eight layers of management, then have nine self-joins. Most databases are pretty good at joining tables, handling even a dozen or two with ease.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
SQL
How to check SQL query construction with the Mimer Validator
Using the SQL GROUP BY clause for counting combinations
How to use an SQL CASE expression
How to sort an SQL UNION query with special ORDER BY sequence
How to use string functions to make an SQL join
An SQL solution for a customer order homework problem
How to use SQL's POSITION function with substrings
Using SQL date functions to get totals for last three days
Using CASE in the SQL ORDER BY clause
What's the difference between an SQL inner join and equijoin?

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts