I am trying to display the employee number of each employee who manages other employees with the number of people he or she manages with the below table called emp.
empno ename job mgr hiredate sal comm deptno
----- ------ ---------- ---------- ---------- ------- ------- ------
7369 Smith Clerk 7902 1980-12-17 800 20
7499 Allen Salesman 7698 1981-02-20 1600 300 30
7521 Ward Salesman 7698 1981-02-22 1250 500 30
7566 Jones Manager 7839 1981-04-02 2975 20
7654 Martin Salesman 7698 1981-09-28 1250 1400 30
7698 Blake Manager 7839 1981-05-01 2850 30
7782 Clark Manager 7839 1981-06-09 2450 10
7788 Scott Analyst 7566 1982-12-09 3000 20
7839 King President 1981-11-17 5000 10
7844 Turner Salesman 7698 1981-09-08 1500 0 30
7876 Adams Clerk 7788 1983-01-12 1100 20
7900 James Clerk 7698 1983-12-03 950 30
7902 Ford Analyst 7566 1983-12-13 3000 20
7934 Miller Clerk 7782 1982-01-23 1300
Any idea of how I can go about doing this?
I have tried
select empno,count(mgr) from emp group by empno,mgr;
but this returns:
empno count(mgr)
---------- ----------
7369 1
7499 1
7521 1
7566 1
7654 1
7698 1
7782 1
7788 1
7839 0
7844 1
7876 1
7900 1
7902 1
7934 1
Thanks so much for your help.
select count(*) from employee_table group by mgr
I would actualy group by mgr
, then you'd have a group per manager and can just do a count to see how many persons that manager manages. Then, you could do a self join on the table to get that manager's info. Something like:
SELECT E1.Mgr, E2.ename, Count(*) as Number FROM Employees E1
INNER JOIN Employees E2 ON E1.mgr = E2.empno
GROUP BY E1.Mgr
Though I haven't tested this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With