总体思路还是形成 员工 部门 员工title 这样的表,然后先对部门进行分组,然后对title ,最后计数
select
dept_no,
dept_name,
title,
count(title) count
from
(select
a.emp_no,
a.title,
b.dept_no,
b.dept_name
from
titles a
left join
(SELECT
a.emp_no,
a.dept_no,
b.dept_name
FROM
dept_emp a
LEFT JOIN departments b ON a.dept_no = b.dept_no)b
on a.emp_no=b.emp_no)a
group by dept_no,title
order by dept_no,title