Oracle -阶梯价计算

需求:阶梯价计算。

如有以下区间价格,当销量落在0-2000区间,单台奖励100元,落在2000-5000时奖励80,.....以此类推。

例如当销量为11000,共奖励:2000*100+(5000-2000)*80+(10000-5000)*60+(11000-10000)*50=790000元

 

 解决方法:

create or replace function fn_caljili(v_usenum2 number, v_product varchar2)
--根据金额计算项目激励
  return number is
  v_money   number(10, 2); --总金额
  cursor cur_xmjl(v_type varchar2) is
    select * from t_mf_cw_cpjzcsb01 where product = v_type order by qujian1;
    v_pricetable t_mf_cw_cpjzcsb01%rowtype;
begin
  v_money := 0;
  for v_pricetable in cur_xmjl(v_product) loop
    if v_pricetable.qujian2 is null or v_usenum2 <= v_pricetable.qujian2 then
      --最后阶梯  (总销量 - 下限值) * 价格
      v_money := v_money + v_pricetable.price*(v_usenum2 - v_pricetable.qujian1);
      exit;
    else
      --非最后阶梯 (上限值-下限值)* 价格
      v_money := v_money + v_pricetable.price*(v_pricetable.qujian2 - v_pricetable.qujian1);
    end if;
  end loop;
  return v_money;
end;

 

posted @ 2022-07-23 15:34  cqyyck  阅读(255)  评论(0编辑  收藏  举报