有一个部门表departments简况如下: dept_no dept_name d001 Marketing d002 Finance 有一个,部门员工关系表dept_emp简况如下: emp_no dept_no from_date to_date 10001 d001 2001-06-22 9999-01-01 10002 d001 1996-08-03 9999-01-01 10003 d002 1996-08-03 9999-01-01 有一个薪水表salaries简况如下: emp_no salary from_date to_date 10001 85097 2001-06-22 2002-06-22 10001 88958 2002-06-22 9999-01-01 10002 72527 1996-08-03 9999-01-01 10003 32323 1996-08-03 9999-01-01 请你统计各个部门的工资记录数,给出部门编码dept_no、部门名称dept_name以及部门在salaries表里面有多少条记录sum,按照dept_no升序排序,以上例子输出如下: dept_no dept_name sum d001 Marketing 3 d002 Finance 1
示例1
输入
drop table if exists `departments` ;
drop table if exists `dept_emp` ;
drop table if exists `salaries` ;
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 `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
INSERT INTO departments VALUES('d001','Marketing');
INSERT INTO departments VALUES('d002','Finance');
INSERT INTO dept_emp VALUES(10001,'d001','2001-06-22','9999-01-01');
INSERT INTO dept_emp VALUES(10002,'d001','1996-08-03','9999-01-01');
INSERT INTO dept_emp VALUES(10003,'d002','1996-08-03','9999-01-01');
INSERT INTO salaries VALUES(10001,85097,'2001-06-22','2002-06-22');
INSERT INTO salaries VALUES(10001,88958,'2002-06-22','9999-01-01');
INSERT INTO salaries VALUES(10002,72527,'1996-08-03','9999-01-01');
INSERT INTO salaries VALUES(10003,32323,'1996-08-03','9999-01-01');
输出
d001|Marketing|3
d002|Finance|1
加载中...