2012-12-05 42 views
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; 

在此先感謝

+0

什麼是你有問題? – Roger

回答

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