In the current project where I am working, I have come across a peculiar situation. Please consider the following table structure:
AGY_AGENCY (
AGENCY_ID NUMBER(9) not null,
AGENCY_CD VARCHAR2(30) not null,
AGC_LEG_CD VARCHAR2(30) not null,
........................
)
AGY_RELN (
AGY_RELN_ID NUMBER(9) not null,
AGENCY_ID NUMBER(9) not null, -- Refers to AGY_AGENCY.AGENCY_ID
RELATIONSHIP_LINK_TYPE VARCHAR2(30) not null,
............................
)
AGY_REL_AGENCY (
REL_AGY_ID NUMBER(9) not null,
AGY_RELN_ID NUMBER(9) not null, -- Refers to AGY_RELN.AGY_RELN_ID
RELN_AGENCY_ID NUMBER(9) not null, -- Refers to AGY_AGENCY.ACY_AGENCY_ID
...............................
)
Below is a sample data
AGY_AGENCY
AGENCY_ID AGENCY_CD AGC_LEG_CD
--------------------------
1000, 'ABC', 'ABC'
1001, 'DEF', 'DEF'
AGY_RELN
AGY_RELN_ID AGENCY_ID RELATIONSHIP_LINK_TYPE
-----------------------------------------------
2000, 1000, 'PARENT_OUTLET'
AGY_REL_AGENCY
REL_AGY_ID AGY_RELN_ID RELN_AGENCY_ID
--------------------------------------------
3000, 2000, 1001
As per this data, Agency "DEF" is a parent outlet agency of "ABC".
I need to formulate a sql query that will return all the parents, grandparents. AGENCY_ID
s (the data can span across multiple levels of hierarchy) starting from a particular AGENCY_ID
.