我试图找出工人在工作2年后可以预期的平均工资增长百分比。
Table workers
+-----------+------------+
| worker_id | hire_date |
+-----------+------------+
| 10001 | 1986-06-26 |
| 10002 | 1996-08-03 |
+-----------+------------+
Table salaries
+-----------+--------+------------+------------+
| worker_id | salary | from_date | to_date |
+-----------+--------+------------+------------+
| 10001 | 60117 | 1986-06-26 | 1987-06-26 |
| 10001 | 62102 | 1987-06-26 | 1988-06-25 |
| 10001 | 66074 | 1988-06-25 | 1989-06-25 |
| 10001 | 66596 | 1989-06-25 | 1990-06-25 |
| 10001 | 66961 | 1990-06-25 | 1991-06-25 |
| 10002 | 65828 | 1996-08-03 | 1997-08-03 |
| 10002 | 65909 | 1997-08-03 | 1998-08-03 |
| 10002 | 67534 | 1998-08-03 | 1999-08-03 |
| 10002 | 69366 | 1999-08-03 | 2000-08-02 |
+-----------+--------+------------+------------+
目前,我有这个查询,但我没有得到任何结果。你能救我吗?
SELECT YEAR(hire_date), YEAR(from_date), MAX(salary)-MIN(salary)
FROM salaries JOIN workers using(worker_id)
WHERE YEAR(hire_date)-YEAR(from_date) = 2;
答案 0 :(得分:0)
你想要雇用某人的工资和两年后的工资。
基本上,你想要为每个工人两次查找薪水,然后执行一些算术:
select avg(s2.salary - s.salary)
from workers w join
salaries s
on s.worker_id = w.worker_id and s.start_date = w.hire_date join
salaries s2
on s2.worker_id = w.worker_id and
w.start_date + interval 2 year >= s2.start_date and
w.start_date + interval 2 year < s2.to_date;
如果你想增加一个百分比,那就更像是:
select avg( (s2.salary / s.salary) - 1)