Is COUNT wrong when grouping?

I am trying to display the employee number of each employee who manages other employees with the number of people with whom he or she manages , with the table below emp .. p>

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 on how I can do this?

I 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 

Many thanks for your help.

+6
source share
3 answers

select count(*) from employee_table group by mgr

+4
source

I would make the current mgr group, then you will have a group for each manager, and you can simply calculate how many people the manager manages. Then you can make an independent connection on the table to get information about the manager. Sort of:

 SELECT E1.Mgr, E2.ename, Count(*) as Number FROM Employees E1 INNER JOIN Employees E2 ON E1.mgr = E2.empno GROUP BY E1.Mgr 

Although I have not tested this.

+3
source

You can try those that don't use join:

 select mgr, count(eno) from employee group by mgr 

or

 select name, e1.mgr, count(e1.eno) from employee e1 group by rollup (e1.mgr, name) 
+1
source

Source: https://habr.com/ru/post/917906/


All Articles