求指导一下这个oracle题目,谢谢!!

卡梅隆BBer 2013-12-15 03:10:33
有两张表,分别是cust_info(客户信息表)和cust_dep_info(客户盈利表)。
cust_info表中有三个字段,分别为cust_id(varchar2),cust_name(varchar2),cust_type(varchar2);
cust_dep_info表中有四个字段,分别是account_id(number),cust_id(varchar2),money(number),mounth(date)。

cust_info 中的cust_id 与cust_dep_info 中的cust_id关联。

存储过程实现:
给出一个日期,然后计算与日期对应的每个客户的money占总money的比例,根据计算出的这个比例,将客户划分成不同级别,然后将级别更新到cust_info表的cust_type字段中。

所占比例及对应的客户类型如下:
[0 - 20%] 核心客户
(20% - 80%]战略客户
(80% - 100]普通客户

使用oracle中的存储过程实现。请大神赐教,菜鸟初入职场,能力有限,必定认真领会,感激不尽!
...全文
112 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
哦 上面有的细节没改好

create or replace procedure your_pro(in_date in date,
                                     RetMemo out varchar2 --若失败,返回失败信息
                                     )
is
 v_cust_type    varchar2(20);
 cust_num number;
 v_count number;
 cursor c_find is
 select cust_id,rank() over(order by cust_money) as rnk from 
 (select cust_id,sum(money) cust_money from cust_dep_info 
  where mounth=in_date group by cust_id );
 cc c_find%rowtype;
begin
  RetMemo :='';
  v_count :=0;
  select count(distinct cust_id) into cust_num from cust_dep_info 
  where mounth=in_date;
  open c_find;
      loop
          fetch c_find into cc;
          exit when c_find%notfound;
          if cc.rnk>=0 and cc.rnk<=round(cust_num*0.2) then
            v_cust_type :='核心客户';
            elsif  cc.rnk>round(cust_num*0.2) and cc.rnk<=round(cust_num*0.8) then
               v_cust_type :='战略客户';
            elsif cc.rnk>round(cust_num*0.8) and cc.rnk<=cust_num then
                   v_cust_type :='普通客户';
                   end if;
          update cust_info set cust_type=v_cust_type where cust_id=cc.cust_id;
          commit;
          v_count :=v_count+1;
          end loop;
          close c_find;
          RetMemo:=RetMemo||'成功更新了'||v_count||'条数据';
exception
   when others then
     begin
      rollback;
      RetMemo := sqlerrm;
      return;
     end;
end;
  • 打赏
  • 举报
回复
#1 很详细,楼主很细致的运用了开区间闭区间。 但是我也奇怪,百分比小的反而是核心客户。 还是说,在全部用户中 每个客户的money占总money的比例 逆序排列以后,取人数总数的百分比? 若是我说的总人数百分比的话可以用下面的sql:[修改自1#]

create or replace procedure your_pro(in_date in date,
                                     RetMemo out varchar2 --若失败,返回失败信息
                                     )
is
 v_cust_type    varchar2(20);
 cust_num number;
 v_count number;
 cursor c_find is
 select cust_id,rank() over(order by cust_money) as rnk from 
 (select cust_id,sum(money) cust_money from cust_dep_info where mounth=in_date group by cust_id );
 cc c_find%rowtype;
begin
  RetMemo :='';
  v_count :=0;
  select count(distinct cust_id) into cust_num from cust_dep_info;
  open c_find;
      loop
          fetch c_find into cc;
          exit when c_find%notfound;
          if cc.rnk>=0 and cc.rnk<=round(cust_num*0.2) then
            v_cust_type :='核心客户';
            elsif  cc.rnk>round(cust_num*0.2) and cc.bfb<=round(cust_num*0.8) then
               v_cust_type :='战略客户';
            elsif cc.bfb>round(cust_num*0.8) and cc.bfb<=cust_num then
                   v_cust_type :='普通客户';
                   end if;
          update cust_info set cust_type=v_cust_type where cust_id=cc.cust_id;
          commit;
          v_count :=v_count+1;
          end loop;
          close c_find;
          RetMemo:=RetMemo||'成功更新了'||v_count||'条数据';
exception
   when others then
     begin
      rollback;
      RetMemo := sqlerrm;
      return;
     end;
end;
_拙计 2013-12-16
  • 打赏
  • 举报
回复
create or replace procedure your_pro(in_date in date,
                                     RetMemo out varchar2 --若失败,返回失败信息
                                     )
is
 v_cust_type    varchar2(20);
 v_count number;
 cursor c_find is
 select cust_id,100*round(e_money/sum(e_money)over(),4) bfb from
 (select cust_id,sum(money) e_money from cust_dep_info where mounth=in_date group by cust_id );
 cc c_find%rowtype;
begin
  RetMemo :='';
  v_count :=0;
  open c_find;
      loop
          fetch c_find into cc;
          exit when c_find%notfound;
          if cc.bfb>=0 and cc.bfb<=20 then
            v_cust_type :='核心客户';
            elsif  cc.bfb>20 and cc.bfb<=80 then
               v_cust_type :='战略客户';
            elsif cc.bfb>80 and cc.bfb<=100 then
                   v_cust_type :='普通客户';
                   end if;
          update cust_info set cust_type=v_cust_type where cust_id=cc.cust_id;
          commit;
          v_count :=v_count+1;
          end loop;
          close c_find;
          RetMemo:=RetMemo||'成功更新了'||v_count||'条数据';
exception
   when others then
     begin
      rollback;
      RetMemo := sqlerrm;
      return;
     end;
end;
不过你这个分客户类型是不是分错了 ?还是我理解错了、、、

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧