snippetMinor
Oracle: How do I query a Hierarchical table?
Viewed 0 times
queryhierarchicalhoworacletable
Problem
Background
This is for the construction of some views we'll be using for reporting.
I have an table of locations, the key fields being "location" and "parent".
The structure that these two fields create, level-wise, are along the lines of Company Name --> Campus Name --> Building Name --> Floor Name --> Room Name. Company name remains the same and Campus name remains the same in this case.
The structure of locations generally looks like this:
Every location links back to its parent location, which is ultimately the organization name. Currently, there is only one organization and one campus.
Goals
This is for the construction of some views we'll be using for reporting.
I have an table of locations, the key fields being "location" and "parent".
The structure that these two fields create, level-wise, are along the lines of Company Name --> Campus Name --> Building Name --> Floor Name --> Room Name. Company name remains the same and Campus name remains the same in this case.
The structure of locations generally looks like this:
+-----------+
| Org. Name |
+-----+-----+
|
+-----v-----+
+--------------------+|Campus Name|+---+--+-------------+
| +--+--------+ | |
| | | |
| | | |
+--+-----+ +------+-+ +--+----+ +---+---+
+--+| BLDG-01|+--+ | BLDG-02| |BLDG-03| |Grounds|
| +--------+ | +--------+ +-------+ +-------+
+-+------+ +-----+--+
|Floor-01| |Basement+-------+
+-+------+ +--------+ |
| |
| |
| +----------+ +-------+--+
+-+Room 1-001| |Room B-002|
+----------+ +----------+Every location links back to its parent location, which is ultimately the organization name. Currently, there is only one organization and one campus.
Goals
- I would like to be able to query all locations beneath any given location at the "Building" level. This is so I can return things like how many workorders have been performed for any location within a given building.
- I would like to be able to determine which sub-location belongs to which building. Essentially the reverse; I would like to go from any level beneath the building level and trace back up to what
Solution
CONNECT BY is the correct way to handle data that is naturally recursive.I don't know what your table looks like but maybe something like:
SELECT *
FROM some_table st
START WITH st.location = 'BLDG-01'
CONNECT BY PRIOR st.location = st.parent;This should get nodes under "BLDG-01".
The
START WITH clause is your base case.Another explanation (aside from the Oracle one which I assume you've already read and had trouble with, it's probably very terse):
http://www.adp-gmbh.ch/ora/sql/connect_by.html
Also:
http://psoug.org/reference/connectby.html
And:
http://www.oradev.com/connect_by.jsp
Code Snippets
SELECT *
FROM some_table st
START WITH st.location = 'BLDG-01'
CONNECT BY PRIOR st.location = st.parent;Context
StackExchange Database Administrators Q#20117, answer score: 9
Revisions (0)
No revisions yet.