我正在编写一个代码,将以下一组表分为4种不同的员工类型:
SQL> select * from Project;
PNAME PNUMBER PLOCATION DNUM
--------------- ---------- --------------- ----------
Virtualization 40 Houston 5
CRM 50 Stafford 4
Bladeservers 60 Houston 1
ProductX 1 Bellaire 5
ProductY 2 Sugarland 5
ProductZ 3 Houston 5
Computerization 10 Stafford 4
Reorganization 20 Houston 1
Newbenefits 30 Bellaire 4
9 rows selected.
SQL> select * from Department;
DNAME DNUMBER MGRSSN MGRSTARTD
--------------- ---------- --------- ---------
Marketing 30 222222222 13-OCT-89
Research 5 333445555 22-MAY-78
Administration 4 987654321 01-JAN-85
Headquarters 1 886655555 19-JUN-71
SQL> select * from Employee;
FNAME M LNAME SSN BDATE ADDRESS S SALARY SUPERSSN DNO
--------------- - --------------- --------- --------- ------------------------------ - ---------- --------- ----------
Theodore M Nixon 333333333 01-JUL-75 100 Coley, Norfolk, VA M 44000 222222222 1
James F Kennedy 444444444 23-OCT-63 299 Lafayette, Ivor, VA M 39000 333333333 1
James E Borg 777777777 10-NOV-27 450 Stone, Houston, TX M 55000 1
John B Smith 123456789 09-JAN-55 731 Fondren, Houston, TX M 30000 333445555 5
Franklin T Wong 333445555 08-DEC-45 638 Voss, Houston, TX M 40000 886655555 5
Alicia J Zelaya 999887777 19-JUL-58 3321 Castle, Spring, TX F 25000 987654321 4
Jennifer S Wallace 987654321 20-JUN-31 291 Berry, Bellaire, TX F 43000 886655555 4
Ramesh K Narayan 666884444 15-SEP-52 975 Fire Oak, Humble, TX M 38000 333445555 5
Joyce A English 453453453 31-JUL-62 5631 Rice, Houston, TX F 25000 333445555 5
Ahmad V Jabbar 987987987 29-MAR-59 980 Dallas, Houston, TX M 25000 987654321 4
James E Modified 886655555 10-NOV-27 450 Stone, Houston, TX M 55000 1
Wilber G Mills 222222222 05-JAN-74 220 Hampton, Norfolk, VA M 20000 123456789 5
12 rows selected.
SQL> select * from Works_On;
ESSN PNO HOURS
--------- ---------- ----------
123456789 1 32.5
123456789 2 7.5
666884444 3 40
453453453 1 20
453453453 2 20
333445555 2 10
333445555 3 10
333445555 10 10
333445555 20 10
999887777 30 30
999887777 10 10
987987987 10 35
987987987 30 5
987654321 30 20
987654321 20 15
886655555 20
666884444 1 10
333445555 1 5
333445555 30 5
19 rows selected.
我需要创建一个SQL查询或一组查询,以查明项目中有多少员工:
我在脑海中有一些问题可以单独获取每个查询,但子集不能重叠。如果一名员工在该部门并且他是一名主管,那么他只能算作一名主管。
这是我的逻辑:
部门:项目编号 - > Works_on pno; ESSN - >员工SSN; DNO - >部门编号(check =)
非部门:与上述相同(检查!=)
主管:与以前相同,但请检查员工SuperSSN
经理:与以前相同,但请查看部门经理
有任何帮助吗?
编辑某些人想看代码:
下面是我的开始代码,以获得项目中的多个独特ppl:
SQL> SELECT pnumber, count(distinct ssn) as Emp_Work FROM project LEFT JOIN (works_on join employee on essn=ssn) ON pnumber=pno GROUP BY pnumber ORDER BY pnumber;
PNUMBER EMP_WORK
---------- ----------
1 4
2 3
3 2
10 3
20 3
30 4
40 0
50 0
60 0
选择了9行。