一定要弄清楚题目的意思,要求的是每个部门的当前员工的title,但是对于每个title还要给出这个title在本部门有多少个。所以分组的依据是部门与title,然后表连接查询就可以了
select a.dept_no,dept_name,title,count
from(
select dept_no,title,count(title) count
from(
select a.dept_no dept_no,title
from dept_emp a,titles b
where a.emp_no=b.emp_no
)a
group by dept_no,title
)a,departments b
where a.dept_no=b.dept_no
order by dept_no,title asc

京公网安备 11010502036488号