26、汇总各个部门当前员工的title类型的分配数目
1、题目描述
汇总各个部门当前员工的title类型的分配数目,结果给出部门编号dept_no、dept_name、其当前员工所有的title以及该类型title对应的数目count
CREATE TABLE `departments` (
`dept_no` char(4) NOT NULL,
`dept_name` varchar(40) NOT NULL,
PRIMARY KEY (`dept_no`));
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE IF NOT EXISTS `titles` (
`emp_no` int(11) NOT NULL,
`title` varchar(50) NOT NULL,
`from_date` date NOT NULL,
`to_date` date DEFAULT NULL);
CREATE TABLE `departments` (
`dept_no` char(4) NOT NULL,
`dept_name` varchar(40) NOT NULL,
PRIMARY KEY (`dept_no`));
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE IF NOT EXISTS `titles` (
`emp_no` int(11) NOT NULL,
`title` varchar(50) NOT NULL,
`from_date` date NOT NULL,
`to_date` date DEFAULT NULL);
输入描述:
无
输出描述:
dept_no | dept_name | title | count |
---|---|---|---|
d001 | Marketing | Senior Engineer | 1 |
d001 | Marketing | Staff | 1 |
d002 | Finance | Senior Engineer | 1 |
d003 | Human Resources | Senior Staff | 1 |
d004 | Production | Senior Engineer | 2 |
d005 | Development | Senior Staff | 1 |
d006 | Quality Management | Engineer | 2 |
d006 | Quality Management | Senior Engineer | 1 |
2、代码:
select t1.dept_no,dp.dept_name,t1.title,t1.count from ( select d.emp_no,d.dept_no,t.title,count(1) as count from dept_emp d inner join titles t on d.emp_no=t.emp_no and d.to_date='9999-01-01' and t.to_date='9999-01-01' group by d.dept_no,t.title ) t1 inner join departments dp on t1.dept_no=dp.dept_no ;
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步