1. 程式人生 > >22. 統計各個部門對應員工漲幅的次數總和

22. 統計各個部門對應員工漲幅的次數總和

題目描述

統計各個部門對應員工漲幅的次數總和,給出部門編碼dept_no、部門名稱dept_name以及次數sum
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`));

輸入描述:

輸出描述:

dept_no dept_name sum
d001 Marketing 24
d002 Finance 14
d003 Human Resources 13
d004 Production 24
d005 Development 25
d006 Quality Management 25
SELECT
	d.dept_no,
	dept.dept_name,
	count(d.emp_no) AS sum
FROM
	dept_emp d
INNER JOIN salaries s ON d.emp_no = s.emp_no
NATURAL LEFT JOIN departments dept
GROUP BY
	d.dept_no;