1. 程式人生 > >27. 給出每個員工每年薪水漲幅超過5000的員工編號emp_no

27. 給出每個員工每年薪水漲幅超過5000的員工編號emp_no

題目描述

給出每個員工每年薪水漲幅超過5000的員工編號emp_no、薪水變更開始日期from_date以及薪水漲幅值salary_growth,並按照salary_growth逆序排列。

提示:在sqlite中獲取datetime時間對應的年份函式為strftime('%Y', to_date)
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`));

輸入描述:

輸出描述:

emp_no

from_date

salary_growth

10003

1995-12-03

24178

10008

1998-03-11

20843

10008

2000-03-10

5997

SELECT
	s2.emp_no,
	s2.from_date,
	(s2.salary - s1.salary) AS salary_growth
FROM
	salaries s1,
	salaries s2
WHERE
	s1.emp_no = s2.emp_no
AND salary_growth > 5000
AND (
	strftime ('%Y', s2.to_date) - strftime ('%Y', s1.to_date) = 1
	OR strftime ('%Y', s2.from_date) - strftime ('%Y', s1.from_date) = 1
)
ORDER BY
	salary_growth DESC;