SQL查询查找直接链接到经理并在银行内拥有帐户的员工

时间:2016-06-29 20:19:15

标签: sql oracle

我试图找到由经理监督并拥有账户的员工 银行,显示员工工作的分支地址 帐户打开的分支地址?

到目前为止,我有这个:

名称类型:

 CREATE TYPE name_type AS object(
 title varchar2(4), 
 firstname varchar2(20), 
 secondname varchar2(20)); 

ADDRESS TYPE:

 CREATE TYPE address_type AS object(
 street varchar2(20),
 city varchar2(20), 
 p_code varchar2(8));

BRANCH TABLE:

CREATE TABLE branches OF branch_type(
branch_ID PRIMARY KEY);

CREATE TYPE BRANCH_TYPE AS OBJECT(
branch_id NUMBER(3),
Address ADDRESS_TYPE);

员工表:

CREATE TABLE employees OF employee_Type(
branch_ID PRIMARY KEY);

CREATE type employee_Type AS object(
branch ref branch_type,
emp_id NUMBER(8),
address ADDRESS_TYPE,
name name_type,
supervisor REF EMPLOYEE_TYPE,
position VARCHAR2(20),
salary   NUMBER(5),
ninum    VARCHAR2(8));

帐户表:

CREATE TABLE account OF account_type(
acc_num PRIMARY KEY);

CREATE type account_type AS object(
branch_id ref branch_Type,
acc_num       NUMBER(8),
acc_type      VARCHAR(20));

CUSTOMER TABLE:

CREATE TABLE customer OF customer_Type
(cust_ID PRIMARY KEY); 

CREATE type customer_Type AS object(
cust_ID NUMBER(8),
address address_type,
name name_type,
ninum VARCHAR2(8));

客户账户表:

CREATE TABLE customer_account OF cust_acc_type;

CREATE type cust_acc_Type AS object(
acc_num ref account_Type,
cust_id ref customer_Type);

这是我正在使用的查询。

SELECT          e.name.firstname    as f_name, 
                e.emp_id            as emp_id, 
                m.name.firstname    as manager, 
                e.SUPERVISOR.emp_id as s_id 
FROM            EMPLOYEES   e 
LEFT OUTER JOIN EMPLOYEES   m   ON  e.SUPERVISOR.emp_id = m.emp_id 
WHERE           e.SUPERVISOR.emp_id IS NOT NULL;

查询显示员工表中的所有员工,他们是主管,我之所以使用is not null的原因是因为银行经理不受任何人的监督,如下所示:

| fname | emp_id | supervisor | s_id |
|-------|--------|------------|------|
| john  | 102    | alison     | 101  |
| chris | 106    | john       | 102  |
| ryan  | 108    | chris      | 106  |
| jack  | 804    | loraine    | 802  |

我在考虑使用ninum来显示员工在银行中的帐户,因为此列的值将具有来自员工表和客户表的匹配结果。

我知道约翰和杰克都在银行内开户。虽然杰克拥有2-1个经常账户和1个储蓄账户。但是在运行查询时都不会显示。

SELECT          e.name.firstname       as f_name, 
                e.BRANCH_ID            as emp_id, 
                m.name.firstname       as manager, 
                e.SUPERVISOR.branch_id as s_id, 
                c.cust_id.cust_id      as cust_id 
FROM            EMPLOYEES           e,
                customer_account    c 
LEFT OUTER JOIN EMPLOYEES           m   ON  e.SUPERVISOR.branch_id = m.BRANCH_ID 
WHERE           e.SUPERVISOR.branch_id IS NOT NULL 
AND             c.CUST_ID.ninum = e.ninum;

但我无法弄清楚如何使用ninum来显示分支机构的工作地址以及员工将拥有的帐户所在的分支地址。

1 个答案:

答案 0 :(得分:1)

SELECT e.name.firstname                    AS fname,
       e.emp_id,
       e.supervisor.name.firstname         AS manager,
       e.supervisor.emp_id                 AS s_id,
       e.branch.address.street             AS emp_works_at_street,
       e.branch.address.city               AS emp_works_at_city,
       e.branch.address.p_code             AS emp_works_at_pcode,
       ca.acc_num.branch_id.address.street AS acct_at_street,
       ca.acc_num.branch_id.address.city   AS acct_at_city,
       ca.acc_num.branch_id.address.p_code AS acct_at_pcode
FROM   employees e
       INNER JOIN
       customer_account ca
       ON ( e.ninum = ca.cust_id.ninum )
WHERE  e.supervisor IS NOT NULL;