oracle游标

drop table tmp_lzw_3283_tar;
create table tmp_lzw_3283_tar
(
servnumbervarchar2(11)
)
;

load data
infile 'tar_3283.txt'
insert into table tmp_lzw_3283_tar
fields terminated by '|'
(
servnumber
)

/*
select count(*),count(distinct servnumber) from tmp_lzw_3283_tar;

COUNT(*) COUNT(DISTINCTSERVNUMBER)
---------- -------------------------
263 254
*/


---关联用户表

----取全品牌用户品牌数据
drop table tmp_lzw_3278_tmp_tb0 purge;
create table tmp_lzw_3278_tmp_tb0 nologging as
select /*+ parallel(c,16) */
distinct servnumber,
subsid,
dictname from dgdm_ods.to_CM_SUBS_SUBSCRIBER_A c
left join (select CODE_DIC_ID,DICTNAME from DGDM_ODS.TO_CODE_Q_CODE_DIC where CODE_GROUP_ID='PROD_SELECTTEL') d on c.prodid=d.code_dic_id
where c.active=1 and status in('US10','US22','US30')
and DEAL_DATE=to_date('20100524','yyyy-mm-dd hh24:mi:ss');
---全量号码
drop table tmp_lzw_3283_tmp_tb0 purge;
create table tmp_lzw_3283_tmp_tb0 nologging as
select /*+ parallel(c,16) parallel(d,16) parallel(e,16)*/
distinct servnumber,
c.subsid,
e.DICTNAME as status,
d.DICTNAME as brand,
c.STARTDATE
from dgdm_ods.to_CM_SUBS_SUBSCRIBER_A c
left join (select CODE_DIC_ID,DICTNAME from DGDM_ODS.TO_CODE_Q_CODE_DIC where CODE_GROUP_ID='PROD_SELECTTEL') d on c.prodid=d.code_dic_id
left join (select CODE_DIC_ID,DICTNAME from DGDM_ODS.TO_CODE_Q_CODE_DIC where CODE_GROUP_ID='US') e on c.status=e.code_dic_id
where c.active=1 and status in('US10','US22','US30')
and DEAL_DATE=to_date('20100525','yyyy-mm-dd hh24:mi:ss');


drop table tmp_lzw_3283_tp1;
create table tmp_lzw_3283_tp1
nologging as
select /*+ parallel(a,16) parallel(b,16) parallel(c,16) */
distinct
a.servnumber,
b.subsid,
b.brand,
b.startdate,
b.status
fromtmp_lzw_3283_tar a,
tmp_lzw_3283_tmp_tb0 b
wherea.servnumber=b.servnumber(+)
;

/*
SQL> select count(*),count(distinct servnumber) from tmp_lzw_3283_tp1;
254| 254
*/


---所有营销方案

drop table tmp_lzw_3283_yxplan1;
create table tmp_lzw_3283_yxplan1
nologging as
select/*+ parallel(a,16) parallel(b,16) parallel(c,16) */
a.servnumber,
c.yxplanid,
c.yxplanname,
b.APPLYOID,
b.startdate,
b.enddate
fromtmp_lzw_3283_tp1 a,
DGDM_ODS.TO_CM_SUBS_PRIVILEGE_A b,
DGDM_ODS.TO_PC_PS_YXPLAN_D c
wherea.subsid=b.subsid
and((b.enddate > sysdate and b.enddate > b.startdate) or b.enddate is null)
andb.privid=c.yxplanid
and b.DEAL_DATE=to_date(to_char(sysdate-1,'yyyymmdd'),'yyyy-mm-dd hh24:mi:ss')
and c.DEAL_DATE=to_date(to_char(sysdate-1,'yyyymmdd'),'yyyy-mm-dd hh24:mi:ss')
;

/*
SQL> select count(*),count(distinct servnumber) from tmp_lzw_3283_yxplan1;
329| 252
*/

drop table tmp_lzw_3283_yxplan2;
create table tmp_lzw_3283_yxplan2
nologging as
select/*+ parallel(a,16) parallel(b,16)*/
a.servnumber,
a.yxplanid,
a.yxplanname,
b.recdate,
a.startdate,
a.enddate
fromtmp_lzw_3283_yxplan1 a,
DGDM_ODS.TO_CS_REC_RECEPTION_I b
wherea.APPLYOID=b.recoid
andb.isrollback=0
andb.status='stcmNml'
;


/*
SQL> select count(*),count(distinct servnumber) from tmp_lzw_3283_yxplan2;
329| 252
*/


---将办理的所有方案转为列

---用游标处理

drop table tmp_lzw_3238_deal;
create table tmp_lzw_3238_deal
(
servnumbervarchar2(11),
yxplannamevarchar2(600)
)
;

declare
v1_servnumber1varchar2(20);
v1_servnumber2varchar2(20);
v1_yxplannamevarchar2(200);

v2_servnumber1varchar2(20);
v2_servnumber2varchar2(20);
v2_yxplannamevarchar2(200);

all_yxplanname varchar2(800);

cursor v1_cursor is
select distinct servnumber from tmp_lzw_3283_yxplan2;

cursor v2_cursor is
select servnumber,yxplanname from tmp_lzw_3283_yxplan2;

begin
open v1_cursor;
loop
fetch v1_cursor into v1_servnumber1;
exit when v1_cursor%notfound;

v2_servnumber1 :=v1_servnumber1;
all_yxplanname := null;

open v2_cursor;
loop
fetch v2_cursor into v1_servnumber2,v1_yxplanname;
exit when v2_cursor%notfound;

v2_servnumber2 := v1_servnumber2;
v2_yxplanname := v1_yxplanname;

if(v2_servnumber1 = v2_servnumber2)
then
if all_yxplanname is null
then
all_yxplanname := v2_yxplanname;
else
all_yxplanname := all_yxplanname||','||v2_yxplanname;
end if;
end if;

end loop;

insert into tmp_lzw_3238_deal values(v2_servnumber1,all_yxplanname);
commit;

close v2_cursor;

end loop;
close v1_cursor;
end;
/


/* ---第二中语法

declare
v1_servnumber1varchar2(11);
v1_servnumber2varchar2(11);
v1_yxplannamevarchar2(200);

v2_servnumber1varchar2(11);
v2_servnumber2varchar2(11);
v2_yxplannamevarchar2(200);

all_yxplanname varchar2(200);

cursor v1_cursor is
select distinct servnumber from tmp_lzw_3283_yxplan2;

cursor v2_cursor is
select servnumber,yxplanname from tmp_lzw_3283_yxplan2
whereservnumber=v2_servnumber1;

begin
open v1_cursor;
loop
fetch v1_cursor into v1_servnumber1;
exit when v1_cursor%notfound;

v2_servnumber1 := v1_servnumber1;
all_yxplanname := null;

open v2_cursor;
loop
fetch v2_cursor into v1_servnumber2,v1_yxplanname;
exit when v2_cursor%notfound;

if v1_yxplanname is not null then
all_yxplanname := all_yxplanname||','||v1_yxplanname;
end if;
end loop;

insert into tmp_lzw_3238_deal values(v2_servnumber1,all_yxplanname);
commit;

close v2_cursor;

end loop;

close v1_cursor;
end;
/

*/


---结果

create table tmp_lzw_3283_result
nologging as
selectdistinct
a.servnumber,
b.yxplanname yucun,
b.recdate,
c.yxplanname all_yxplan
from tmp_lzw_3283_tp1 a
left join tmp_lzw_3283_yxplan2 b on a.servnumber=b.servnumber and yxplanid in ('75900000090358','75900000090359','75900000090362','75900000090363','75900000090366','75900000090367','75900000090368','75900000090369','75900000090356','75900000090357','75900000090360','75900000090361','75900000090364','75900000090365')
left join tmp_lzw_3238_deal c on a.servnumber=c.servnumber
;

/*
SQL> select count(*),count(distinct servnumber) from tmp_lzw_3283_result;
254| 254
*/

---导出结果

alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
set pagesize 0;
SET NEWPAGE NONE HEADING OFF SPACE 0 PAGESIZE 0 TRIMOUT ON TRIMSPOOL ON LINESIZE 2500 colsep | feedback off termout off pages 0

spool 'lzw_3283_result.txt'

selectservnumber||'|'||
yucun||'|'||
recdate||'|'||
all_yxplan||'|'
fromtmp_lzw_3283_result
;

spool off;

drop table tmp_lzw_3283_tar;
drop table tmp_lzw_3283_tp1;
drop table tmp_lzw_3283_yxplan1;
drop table tmp_lzw_3283_yxplan2;
drop table tmp_lzw_3238_deal;
drop table tmp_lzw_3283_result;

exit;

你可能感兴趣的:(oracle,sql,C++,c,C#)