I have this table named "AFP"
:
ID || NAME || AFP_FUSION_ID
10 || afp1 ||
20 || afp2 ||
30 || afp3 || 90
40 || afp4 || 30
50 || afp5 || 30
90 || afp6 ||
where "ID" is a primary key
and "AFP_FUSION_ID" is a foreign key
that references to the same table.
Also I have this table called "Registers"
:
Full Name || AFP_DESTINO
JOHN || 10
PETER || 20
ELENA || 10
MARCO || 90
DANIELA || 30
FELIPE || 40
where
AFP_DESTINO is a foreign key that references to AFP.ID
I have to create a function that has like parameter a number (10, 20, 30 , 40, 50 or 90) and has to return the corresponding information.
- If the function receives '10' ,the function has to return the data
from "REGISTERS" where AFP_DESTINO = 10
. - If the function receives '30' has to return the data
where AFP_DESTINO = 30, 40 or 50
. - And if the function receives '90', has to return the data
where AFP_DESTINO = 90, 30, 40, 50
.
I have to use Oracle, I have this:
select * from (select * from registers
full outer join afp
on registers.AFP_DESTINO = afp.ID) where afp_destino = 30 or afp_fusion_id = 30
(I have to improve this and transform to a function later). The problem with that is if I substitute '30' for '90', I will not have data when afp_destino = 40 or 50
.
Can somebody help me, please?
UPDATE:
I have this code for resolve this:
select * from (select * from registers
full outer join afp
on registers.AFP_DESTINO = afp.ID) connect by prior
afp_destino = afp_fusion_id start with afp_destino = 90
but generates inconsistencies, Appear more rows.