日期:2014-05-17 浏览次数:20956 次
merge into cust_info using
(select t1.cust_id,
case when t1.cust_tot/t2.tot_money<=0.2 then '核心客户'
when t1.cust_tot/t2.tot_money>0.2 and t1.cust_tot/t2.tot_money<=0.8 then '战略客户'
when t1.cust_tot/t2.tot_money>0.8 then '普通客户'
end cals
from
(select cust_id,sum(money) cust_tot where mounth=pi_dt group by cust_id) t1,
(select sum(money) tot_money from cust_dep_info where mounth=pi_dt) t2) t3
on (cust_info.cust_id=t3.id)
when matched then
update set cust_info.cust_type=t3.cals
--建表和插入数据
create table cust_dep_info
(account_id number,
cust_id varchar2(20),
money number,
month date);
create table cust_info (
cust_id varchar2(20) primary key,
cust_name varchar2(50),
cust_type varchar2(20)
);
insert into cust_info select level, 'Name'
------解决方案--------------------
to_char(level), null
from dual connect by level <= 100;
commit;
insert into cust_dep_info
select level, level, round(DBMS_RANDOM.VALUE(1,1000)), trunc(sysdate) - mod(level, 7)
from dual
connect by level <= 100;
commit;
--Merge,假设给定日期就是今天。
merge into cust_info
using (
with all_cust as (
select count(*) cust_count from cust_dep_info
where month=trunc(sysdate)
),
ranking (core, ordinary) as (
select trunc(cust_count*0.2), trunc(cust_