如何编写一个计算经理部门雇员人数的SQL查询?
问题描述:
列出该经理部门的“经理姓名”和“雇员人数”。
我是SQL新手,在编写查询上述语句的语句时遇到了一些麻烦。
这是我写的陈述,但是当只有3位经理时,它询问6名员工作为经理。
SELECT b.ENAME AS "Manager", COUNT(*) AS "Number of Employees"
FROM EMP e
JOIN EMP b ON b.EMPNO = e.MGR
GROUP BY b.EMPNO, b.ENAME;
主要问题是如何将它写入仅查询3经理和员工数?
感谢您的帮助提前
答
select count(distinct e.mgr), e.ename
from emp e
where e.mgr is not null;
尝试使用不同的和经理不为空。
+0
当你使用count(*)时,也计数为空值 –
你怎么知道谁是经理? – HoneyBadger
你的表格是什么样子的,你有样品数据吗?我们需要更多的帮助 – FMashiro
它是在mysql还是microsoft sql server?标记它approrpiate –