下表结构包含一个族中的关系层次结构(直到第4级适用。例如,层次结构可以从1开始,以1.4.4.4结束)。
King是家庭中的第一个成员(1),而Jones是他的第一个儿子(1.1),而Clark是他的第三个儿子(1.3)。
同样,艾伦是国王的第二个儿子(布莱克)的第一个儿子(1.2.1)等等。NAME | RELATION HIERARCHY
------ ------------------
King | 1
Jones | 1.1
Scott | 1.1.1
Adams | 1.1.1.1
Ford | 1.1.2
Smith | 1.1.2.1
Blake | 1.2
Allen | 1.2.1
Ward | 1.2.2
Clark | 1.3
Miller | 1.3.1
挑战在于建立家庭中任何两个特定成员之间的关系。例如,考虑以下输出。
Member1 | Member2 |Establish Relationship of Member 1 to Member 2
--------------------------------------------------------------------
Jones |Allen |Father's elder brother
Clark |Scott |Father's second younger brother
我是编写查询的新手。
create function Relation(In Member1 Varchar2(20),In Member2 Varchar2(20))
as
Mem1 varchar2(50)
Mem2 varchar2(50)
Path varchar2(50)
T number;
Begin
Select rel_Hier into Mem1 from Rel where Name = Member1;
Select rel_Hier into Mem2 from Rel where Name = Member2;
if((substr(Mem1),1,1)=(substr(Mem2),1,1)) /* Checking whether first number is same in both the members */
Then
if((substr(Mem1),3,1)=(substr(Mem2),3,1))/* Checking whether third number is same in both the members */
Then
if((substr(Mem1),5,1)=(substr(Mem2),5,1)) /* Checking whether fifth number is same in both the members */
Then
T := 5;
End If;
Else
T:=3;
End If;
End if;
path : = reverse(substr(mem1,t))||,||substr(mem2,t);
find_relation(path);
End;
/
create procedure Find_Relation ( Path varchar2(20))
as
Sol varchar2(50);
var1 varchar2(30);
i number;
Begin
i:=0;
while(i< substr(path,','))
Loop
Select relation into var1 from Rel_Tb_Father where id=substr(Path,i,3);
Sol := Sol ||var1||"'s";
i:=i+2;
End Loop
var1:=Select relation from Rel_Tb_Father where id=substr(Path,substr(path,',')-1,3);
Sol = Sol ||var1||"'s";
i:=substr(path,',')+1;
while(i> substr(path,',') and i<length(path)))
Loop
var1:=Select relation from Rel_Tb_Son where id=substr(Path,i,3);
Sol := Sol ||var1||"'s";
i:=i+2;
End Loop
var1:=Select relation from Rel_Tb_Son where id=substr(Path,length(Path)-3);
Sol := Sol ||var1||"'s";
dbms_output.put_line(Sol);
End;
/
我知道这不会起作用,因为它包含很多语法和逻辑错误。如何处理此代码?