I am struggling to write a performing query which would consist of the data from one sub-select, and hierarchically retrieved data from another table based on the rows from that first sub-select.
So, I have some data retrieved from multiple tables with joins, which finally boils down to the following:
CREATE TABLE TBL1 (UUID, MiscData, KeyToLookup, ConditionClause ) AS
SELECT 13, 'ATM', 12345, null FROM DUAL UNION ALL
SELECT 447, 'Balance Inquiry', 67890, 'BALANCE_INQUIRY_FEE' FROM DUAL UNION ALL
SELECT 789, 'Credit', 22321, 'CREDIT_FEE' FROM DUAL;
Now, I have another table which stores the hierarchical structure of fees:
CREATE TABLE TBL2 ( TariffDomainID, FeeType, UpperTariffDomainID, ID ) AS
SELECT 1543, 'WHATEVER_FEE', 154, 1 FROM DUAL UNION ALL
SELECT 1543, 'BALANCE_INQUIRY_FEE', 154, 2 FROM DUAL UNION ALL
SELECT 154, 'SMTHELSE_FEE', 15, 3 FROM DUAL UNION ALL
SELECT 154, 'CREDIT_FEE', 15, 4 FROM DUAL UNION ALL
SELECT 15, 'BALANCE_INQUIRY_FEE', null, 5 FROM DUAL;
And there is a way to link the first selection to the lowest row in hierarchy of the second table, there are few joins but finally it's like this:
CREATE TABLE TBL3 ( ID, FirstTblKey, SecondTblKey ) AS
SELECT 1, 67890, 1543 FROM DUAL UNION ALL
SELECT 2, 22321, 1543 FROM DUAL;
The important point is that it's not guaranteed there will be a row with this KeyToLookup
directly in the second table, as directed by the TBL3
.
E.g. in the example above:
row TBL1.UUID=789
is linked via TBL3
to TBL2
row with TariffDomainID=1543
,
but there is no row in TBL2
with TariffDomainID=1543
and FeeType=CREDIT_FEE
;
however TBL2
contains a link to the same table but upper level, UpperTariffDomainID=154
,
and there is a row in TBL2
with TariffDomainID=154
and FeeType=CREDIT_FEE
.
In the end I need to connect the info from TBL1
with the all occurrences of this key in TBL2
hierarchically, numerated by depth of hierarchy.
So I expect to get this:
| UUID | MiscData | KeyToLookup | ConditionClause | TariffDomainIDWithPresence | Depth |
|------|-----------------|-------------|---------------------|----------------------------|-------|
| 13 | ATM | 12345 | null | null | null |
| 447 | Balance Inquiry | 67890 | BALANCE_INQUIRY_FEE | 1543 | 1 |
| 447 | Balance Inquiry | 67890 | BALANCE_INQUIRY_FEE | 15 | 3 |
| 789 | Credit | 22321 | CREDIT_FEE | 154 | 2 |
Could anyone please teach me how to make such a hierarchical query?