员工数量有困难

时间:2014-03-19 20:58:31

标签: sql oracle

在下表中,我试图找到以下声明:对于每个员工,显示每个员工的监督人数?

NAME                SSN       BDATE     SEX     SALARY SUPERSSN  DNO
------------------- --------- --------- --- ---------- --------- --------
John B Smith        123456789 09-JAN-55 M        30000 333445555 5
Franklin T Wong     333445555 08-DEC-45 M        40000 888665555 5
Alicia J Zelaya     999887777 19-JUL-85 F        25000 987654321 4
Jennifer S Wallace  987654321 20-JUN-31 F        43000 888665555 4
Ramesh K Narayan    666884444 15-SEP-52 M        38000 333445555 5
Joyce A English     453453453 31-JUL-62 F        25000 333445555 5
Ahmad V Jabbar      987987987 29-MAR-59 M        25000 987654321 4
James E Borg        888665555 10-NOV-27 M        55000           

我可以找到员工和主管,但不知道如何计算:

SQL> select e.name, s.name, count(*) from jps.employee e, jps.employee s where e.superssn = s.ssn group by e.name, s.name; 

NAME                NAME                  COUNT(*)
------------------- ------------------- ----------
Ahmad V Jabbar      Jennifer S Wallace           1
Joyce A English     Franklin T Wong              1
Alicia J Zelaya     Jennifer S Wallace           1
Jennifer S Wallace  James E Borg                 1
Franklin T Wong     James E Borg                 1
John B Smith        Franklin T Wong              1
Ramesh K Narayan    Franklin T Wong              1

从上面的结果中,您可以看到富兰克林有3名他监督的员工。我怎么算他们?

提前谢谢你, 尼尔斯。

3 个答案:

答案 0 :(得分:0)

您可以使用" GROUP BY"函数使用Name列(在您的情况下是e.name或s.name)。

有关详细信息,请参阅此link

答案 1 :(得分:0)

我找到了这个例子:Counting child for parent我觉得这就是解决方案:

SQL> select s.name, count(e.name) from jps.employee e, jps.employee s where e.superssn = s.ssn group by s.name;

答案 2 :(得分:0)

SELECT COUNT(E.NAME) AS ENAME,  S.NAME
FROM JPS.EMPLOYEE E INNER JOIN JPS. EMPLOYEE S 
ON E.SUPERSSN = S.SSN
GROUP BY  S.NAME