我正在尝试根据以前的记录计算百分比增长。说,我有这样的记录
----------------------------------------------------------------------------------
| user_id | salary | salary_date_change | percent_increase | salary_record_id |
----------------------------------------------------------------------------------
| 10001 | 10000 | 01.10.2012 | | 1 |
| 10001 | 8000 | 01.09.2012 | | 2 |
| 10001 | 2000 | 01.07.2012 | | 3 |
| 10001 | 4000 | 01.08.2012 | | 4 |
| 10002 | 7500 | 01.09.2012 | | 5 |
| 10002 | 15000 | 01.10.2012 | | 6 |
---------------------------------------------------------------------------------
我想运行更新查询或过程并执行以下操作:
----------------------------------------------------------------------------------
| user_id | salary | salary_date_change | percent_increase | salary_record_id |
----------------------------------------------------------------------------------
| 10001 | 10000 | 01.10.2012 | 20 | 1 |
| 10001 | 8000 | 01.09.2012 | 50 | 2 |
| 10001 | 2000 | 01.07.2012 | 50 | 3 |
| 10001 | 4000 | 01.08.2012 | | 4 |
| 10002 | 7500 | 01.09.2012 | | 5 |
| 10002 | 15000 | 01.10.2012 | 50 | 6 |
---------------------------------------------------------------------------------
我该怎么做?
这是我尝试过的:
create or replace procedure salary_increase_migration (
p_user_id integer,
l_value varchar2 default null,
l_old_salary number default null
) is
l_base_salary_prev_count integer default 0;
CURSOR get_base_salary_prev IS
SELECT *
FROM arr.user_multi_base_salary
ORDER BY salary_date_change DESC;
BEGIN
FOR x IN get_base_salary_prev LOOP
IF l_old_salary IS NOT NULL THEN
l_value := ((x.salary/l_old_salary)-1)*100;
UPDATE arr.user_multi_base_salary
SET percent_increase = l_value
WHERE user_id = x.user_id
AND salary_record_id = x.salary_record_id;
l_old_salary := x.salary;
END IF;
END LOOP;
END;
提前致谢