牛客SQL题解-汇总各个部门当前员工的title类型的分配数目

题目描述

有一个部门表departments简况如下:
 
有一个,部门员工关系表dept_emp简况如下:
 
 
有一个职称表titles简况如下:
 
 
汇总各个部门当前员工的title类型的分配数目,即结果给出部门编号dept_no、dept_name、其部门下所有的员工的title以及该类型title对应的数目count,结果按照dept_no升序排序
 

答案详解

解法一

select dept_emp.dept_no,dept_name,titles.title,count(titles.title)
from dept_emp,titles,departments
where dept_emp.dept_no=departments.dept_no
and dept_emp.emp_no=titles.emp_no
and titles.to_date='9999-01-01'
and dept_emp.to_date='9999-01-01'
group by dept_emp.dept_no,titles.title
order by dept_emp.dept_no asc

解法二

select dept_emp.dept_no,dept_name,titles.title,count(titles.title)
from dept_emp join departments 
on dept_emp.dept_no=departments.dept_no and dept_emp.to_date='9999-01-01'
join titles
on  dept_emp.emp_no=titles.emp_no and titles.to_date='9999-01-01'
group by dept_emp.dept_no,titles.title
order by dept_emp.dept_no asc

  

原文地址:https://www.cnblogs.com/Bluebells/p/14516782.html