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:
+-----------+
| 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 the building is.
- I would like this to be in a view. That means, I would like to have a table that for every item at the "building" level, lists the building in the left-hand column and all possible locations UNDER that building in the right-hand column. This way I'd have a list that I could query at any time to find which locations are part of which building.
Attempts and Doing it Right
I've attempted to do this through horribly constructed views, UNION queries, etc. -- which all have seemed like a bad idea. I know Oracle possesses a mechanism for this through "CONNECT BY"; I'm just not sure how to make use of it.
NULL
for them? How do you identify a "building level"?