从以前的记录计算百分比

Posted

技术标签:

【中文标题】从以前的记录计算百分比【英文标题】:percentage calculation from previous record 【发布时间】:2012-12-05 11:24:58 【问题描述】:

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

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

提前致谢

【问题讨论】:

您遇到了什么问题? 【参考方案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

【讨论】:

【参考方案2】:

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

类似

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

【讨论】:

以上是关于从以前的记录计算百分比的主要内容,如果未能解决你的问题,请参考以下文章

从多列值计算百分比

利用Java实现断点上传

SQL:计算每个设备集连续出现相同值的所有记录并返回最高计数:百分比

计算百分比时抛出OverflowException

SQL:计算输出列上出现的次数并根据出现次数计算一些百分比

从 shapefile 计算百分比覆盖率