SQL---统计出当前各个title类型对应的员工当前薪水对应的平均工资。结果给出title以及平均工资avg。

统计出当前各个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);

代码:

select title,avg(salary) as avg 
from salaries as sa,titles as t
where sa.emp_no = t.emp_no  #找到员工
and sa.to_date = '9999-01-01'
and t.to_date = '9999-01-01'
group by t.title   #按照title分组

猜你喜欢

转载自blog.csdn.net/qq_40637313/article/details/89061082