drop table node; create table node ( id number primary key ,op varchar2(2) ,value number ,parent references node ,parse varchar2(50) ,carry number ); insert into node values (1,'*' ,NULL,NULL,NULL,NULL); insert into node values (2,'+' ,NULL,1 ,NULL,NULL); insert into node values (3,'-' ,NULL,1 ,NULL,NULL); insert into node values (4,NULL,1 ,2 ,NULL,NULL); insert into node values (5,NULL,3 ,2 ,NULL,NULL); insert into node values (6,NULL,4 ,2 ,NULL,NULL); insert into node values (7,NULL,4 ,3 ,NULL,NULL); insert into node values (8,NULL,5 ,3 ,NULL,NULL); drop view pnode; drop view enode; create view enode as ( select n.id,n.op,n.value,n.parent,n.carry from node n left outer join node p on n.parent = p.id where n.carry is null and p.carry is null UNION select n.id,NULL,n.carry,n.parent,n.carry from node n left outer join node p on n.parent = p.id where n.carry is not null and p.carry is null ); create view pnode as ( select n.id,n.op,cast(n.value as varchar2(50)) value,n.parent,n.parse from node n left outer join node p on n.parent = p.id where n.parse is null and p.parse is null UNION select n.id,NULL,n.parse,n.parent,n.parse from node n left outer join node p on n.parent = p.id where n.parse is not null and p.parse is null );