I have sample data that shows a hierarchy between labs (locations).
CREATE TABLE "DEMO"."LABS"
(
"LABID" VARCHAR2(20 BYTE),
"PARENT" VARCHAR2(20 BYTE)
)
A lab has a single parent and therefore n ancestors. I found a way to figure out the entire list of ancestors given a specific lab ID on Oracle's website.
SELECT LABID
FROM LABS
START WITH LABID = 111
CONNECT BY PRIOR LABS.PARENT = LABS.LABID;
Now I would like to create a view from it that iterates through all the lab identifiers.
I tried a few things (including a LEFT JOIN) but never quite managed to get the right result.
If my table contains:
-----------------
| LAB_ID | PARENT |
-----------------
| 1 | NULL |
| 11 | 1 |
| 111 | 11 |
-----------------
I would like a VIEW that gives me:
-------------------
| LAB_ID | ANCESTOR |
-------------------
| 1 | NULL |
| 11 | 1 |
| 111 | 1 |
| 111 | 11 |
-------------------
Notice how, now, I get 2 entries for lab 111. The whole point of the exercise is I want to be able to have a simple way of querying the data i.e.:
SELECT ANCESTOR FROM my_view WHERE LAB_ID = ?
I noticed this question and answer but I failed to fully understand it.