Oracle 语句,查询出scott用户下的每个部门的员工数,要求没有员工的部门,列出0

2025-03-23 10:52:10
推荐回答(2个)
回答1:

select a.dname,
sum(case when a.deptno=b.deptno then 1 else 0 end) 人数
from dept a left join emp b on a.deptno=b.deptno
group by a.dname

回答2:

--try this sql
select A.DEPTNO, a.dname, count(B.EMPNO) "COUNT"
  from dept a
  left join emp b
    on a.deptno = b.deptno
 group by a.dname, A.DEPTNO
 ORDER BY A.DEPTNO;