`

sql行列转换

 
阅读更多

--目标号码导入
select count(*),count(distinct servnumber) from wam_temp_data;

--取用户时间、是否托收,托收开始时间
create table wam_temp_rec1 as
select b.custid,b.subsid,b.servnumber,b.acctid,b.startdate kaihu_date,c.startdate tuoshou_date
from wam_temp_data a,zjyy.cm_subs_subscriber b,zjzw.cm_ca_settletype c
where a.servnumber=b.servnumber
andb.active='1'
and b.acctid=c.acctid(+)
and c.status(+)='1'
and c.paytype(+)='sttpBank';


--取号码,时间,月份,已销账金额
create table wam_temp_rec2 as
select c.servnumber,c.kaihu_date,c.month_fee,sum(c.recamt) recamt
from (select a.servnumber,a.kaihu_date,substr(b.validbillcyc,1,6) month_fee,b.recamt
from wam_temp_rec1 a,zjzw.ib_cbb_woffcust b
where a.subsid=b.subsid
union all
select a.servnumber,a.kaihu_date,substr(b.validbillcyc,1,6) month_fee,b.recamt
from wam_temp_rec1 a,zjzwhis.ib_cbb_woffcust b
where a.subsid=b.subsid) c
group by c.servnumber,c.kaihu_date,c.month_fee;

SET NEWPAGE NONE HEADING OFF SPACE 0 PAGESIZE 0 TRIMOUT ON TRIMSPOOL ON LINESIZE 2500 colsep | feedback off termout off pages 0
spool wam_temp_result.txt
select servnumber||'|'||to_char(kaihu_date,'yyyy-mm-dd hh24:mi:ss')||'|'||
sum(case when month_fee='200608' then recamt else 0 end)||'|'||
sum(case when month_fee='200609' then recamt else 0 end)||'|'||
sum(case when month_fee='200610' then recamt else 0 end)||'|'||
sum(case when month_fee='200611' then recamt else 0 end)||'|'||
sum(case when month_fee='200612' then recamt else 0 end)||'|'||
sum(case when month_fee='200701' then recamt else 0 end)||'|'||
sum(case when month_fee='200702' then recamt else 0 end)||'|'||
sum(case when month_fee='200703' then recamt else 0 end)||'|'||
sum(case when month_fee='200704' then recamt else 0 end)||'|'||
sum(case when month_fee='200705' then recamt else 0 end)||'|'||
sum(case when month_fee='200706' then recamt else 0 end)||'|'||
sum(case when month_fee='200708' then recamt else 0 end)||'|'||
sum(case when month_fee='200709' then recamt else 0 end)||'|'||
sum(case when month_fee='200710' then recamt else 0 end)||'|'||
sum(case when month_fee='200711' then recamt else 0 end)||'|'||
sum(case when month_fee='200712' then recamt else 0 end)||'|'||
sum(case when month_fee='200801' then recamt else 0 end)||'|'||
sum(case when month_fee='200802' then recamt else 0 end)||'|'||
sum(case when month_fee='200803' then recamt else 0 end)||'|'||
sum(case when month_fee='200804' then recamt else 0 end)||'|'||
sum(case when month_fee='200805' then recamt else 0 end)||'|'||
sum(case when month_fee='200806' then recamt else 0 end)||'|'||
sum(case when month_fee='200807' then recamt else 0 end)||'|'||
sum(case when month_fee='200808' then recamt else 0 end)||'|'||
sum(case when month_fee='200809' then recamt else 0 end)||'|'||
sum(case when month_fee='200810' then recamt else 0 end)||'|'||
sum(case when month_fee='200811' then recamt else 0 end)||'|'||
sum(case when month_fee='200812' then recamt else 0 end)||'|'||
sum(case when month_fee='200901' then recamt else 0 end)||'|'||
sum(case when month_fee='200902' then recamt else 0 end)||'|'||
sum(case when month_fee='200903' then recamt else 0 end)||'|'||
sum(case when month_fee='200904' then recamt else 0 end)||'|'||
sum(case when month_fee='200905' then recamt else 0 end)||'|'
from wam_temp_rec2
group by servnumber,kaihu_date;
spool off;

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics