I post this as an aid to a question I see posted in this forum and others. The samples posted here are simple and for the purpose of giving guidance to joining a table to itself. I have used this method with a database that included and entire parts and assembly of an aircraft. It was used to provided not only assembly but how one part affected other parts.
***** Create each index for EMP *****
SET COLLATE TO 'MACHINE'
INDEX ON EMPID TAG EMPID CANDIDATE
INSERT INTO emp (empname,empid) VALUES ('john doe',100)
INSERT INTO emp VALUES ('wayne smith' ,101,100)
INSERT INTO emp VALUES ('doris jones' ,102,100)
INSERT INTO emp VALUES ('martha comarow',103,102)
INSERT INTO emp VALUES ('jane doe' ,104,101)
INSERT INTO emp VALUES ('mark gill' ,105,101)
INSERT INTO emp VALUES ('jake brawn' ,106,102)
INSERT INTO emp VALUES ('jill band' ,107,101)
INSERT INTO emp VALUES ('robert diggs' ,108,102)
WAIT WINDOW "display all employees and report to"
select emp.empname as employee, sup.empname as manager ;
from emp ;
join emp as sup on emp.empreport2 = sup.empid
WAIT WINDOW "Display all employees and report to with Top Level"
select emp.empname as employee, sup.empname as manager ;
from emp ;
left join emp as sup on emp.empreport2 = sup.empid
WAIT WINDOW "Select manager and suborinates"
select emp.empname as employee, sup.empname as manager ;
from emp INNER join emp as sup ;
on emp.empreport2 = sup.empid ;
WHERE EMP.EMPID = 102 OR EMP.EMPREPORT2 = 102
INSERT INTO car (partname,partid) VALUES ('auto',100)
INSERT INTO car VALUES ('engine' ,101,100)
INSERT INTO car VALUES ('body' ,102,100)
INSERT INTO car VALUES ('carbarator' ,103,101)
INSERT INTO car VALUES ('air filter' ,104,101)
INSERT INTO car VALUES ('fuel pump' ,105,101)
INSERT INTO car VALUES ('front window',106,102)
INSERT INTO car VALUES ('front seat' ,107,102)
INSERT INTO car VALUES ('carpet' ,108,102)
WAIT WINDOW "display all parts and assembly"
select car.partname as carpart, assem.partname as assembly ;
from car join car as assem on car.memberof = assem.partid
WAIT WINDOW "Display all parts and assemblys with top assembly"
select car.partname as carpart, assem.partname as assembly ;
from car left join car as assem on car.memberof = assem.partid
WAIT WINDOW "Select assembly and parts"
select car.partname as carpart, assem.partname as assembly ;
from car left join car as assem on car.memberof = assem.partid;
WHERE car.partID = 102 OR car.memberof = 102
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.