1. 程式人生 > >16. 統計出當前各個title型別對應的員工當前薪水對應的平均工資

16. 統計出當前各個title型別對應的員工當前薪水對應的平均工資

題目描述

統計出當前各個title型別對應的員工當前薪水對應的平均工資。結果給出title以及平均工資avg。
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`));
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);

輸入描述:

輸出描述:

title

avg

Engineer

94409.0

Senior Engineer

69009.2

Senior Staff

91381.0

Staff

72527.0

SELECT
	t.title,
	avg(s.salary) AS `avg`
FROM
	salaries s
INNER JOIN titles t ON s.emp_no = t.emp_no
WHERE
	s.to_date = '9999-01-01'
AND t.to_date = '9999-01-01'
GROUP BY
	t.title;