1

我正在尝试根据以前的记录计算百分比增长。说,我有这样的记录

----------------------------------------------------------------------------------
| 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;

提前致谢

4

2 回答 2

1

这应该是一个简单的更新。给定表中的一条记录,选择前一条记录应该非常简单(特别是如果所需的记录总是提前一天)。

就像是

 update my_table tgt
 set    percent_increase = (
          select (tgt.salary-src.salary)/case when src.salary = 0 then null else src.salary end
          from   my_table src
          where  src.user_id = tdt.user_id and 
                 src.salary_date_change = (
                   select max(src2.salary_date_change)
                   from   my_table src2
                   where  src2.user_id = src.user_id and
                          src2.salary_date_change < src.salary_date_change))
于 2012-12-05T11:34:34.780 回答
1

计算您可以使用的百分比:

select 
  user_id, salary, salary_change_date, 
  salary/(lag(salary) over (partition by user_id order by salary_change_date))*100 as percent_increase
from user_multi_base_salary;

更新 base_table:

merge into user_multi_base_salary t using(    
      select 
         user_id, salary, salary_change_date, 
         salary/(lag(salary) over (partition by user_id order by salary_change_date))*100 as percent_increase
      from user_multi_base_salary;
    )S
on (s.user_id = t.user_id and s.salary_change_date = t.salary_change_date)
when matched then update set
T.percent_increase = S.percent_increase
于 2012-12-05T11:38:00.873 回答