I need a function which returns description of level from cascade (hierarchy) tables.
CREATE TABLE level_1(
id_1 SERIAL,
description_1 text
);
CREATE TABLE level_2(
id_2 SERIAL,
description_2 text
);
CREATE TABLE level_3(
id_3 int4 SERIAL,
description_3 text
);
CREATE TABLE levels(
id_levels int4 SERIAL,
id_1 int4,
id_2 int4,
id_3 int4
);
I need this function:
SELECT f_get_level('Level 1', 10);
'Level 1'
.. which level I want
10
.. value of id_levels
With this result:
description_1
The table levels
is cross table between the others. Example:
Level 1 - Site
Level 2 - Sectors in Site
Level 3 - Lines in Sector
Levels: Level 1 - Level 2 - Level 3
id1 - Site (S1) - Sector (NULL) - Line (NULL)
id3 - Site (S1) - Sector (Sec1) - Line (NULL)
id4 - Site (S1) - Sector (Sec1) - Line (L1)
id5 - Site (S1) - Sector (Sec1) - Line (L2)
id6 - Site (S1) - Sector (Sec1) - Line (L3)
id7 - Site (S1) - Sector (Sec2) - Line (NULL)
id8 - Site (S1) - Sector (Sec2) - Line (L1)
id9 - Site (S1) - Sector (Sec2) - Line (L2)
id10 - Site (S2) - Sector (NULL) - Line (NULL)
id11 - Site (S2) - Sector (Sec1) - Line (NULL)
I need a function where parameters are id
and Level
.
Examples:
Select f_get_level('Sector', 5) -- where 5 is id5
Result: 'Sec1'
Select f_get_level('Site', 11) -- where 11 is id11
Result: 'S2'
Select f_get_level('Line', 6) -- where 6 is id6
Result: 'L3'
Select f_get_level('Sector', 6) -- where 6 is id6
Result: 'Sec1'