--创建临时表,规定好格式,是必须的,不同于sqlserver那么随意;
Create Global Temporary Table record4
(
yljgdm VARCHAR2(22) not null,
jzlsh VARCHAR2(50) not null,
kh VARCHAR2(32),
klx VARCHAR2(16),
hzxm VARCHAR2(32),
jzlx VARCHAR2(3),
zzysgh VARCHAR2(16),
zzysxm VARCHAR2(32),
jzksmc VARCHAR2(30),
jlgxsj DATE default Sysdate,
zbdm VARCHAR(22)
)
on commit preserve rows;
Create Global Temporary Table fee
(
jzlsh VARCHAR2(50) not null,
mxxmje NUMBER(15,3) default 0,
tfbz VARCHAR2(1) not null
)
on commit preserve rows;
--运行经过调试的脚本,插入数据到临时表中
insert into record4(yljgdm,jlgxsj,zzysgh,zzysxm,jzksmc,zbdm,jzlsh,kh,klx,hzxm,jzlx)
select A.YLJGDM,A.JLGXSJ,A.ZZYSGH,A.ZZYSXM,A.JZKSMC,'MJZYZB',A.JZLSH,A.KH,A.KLX,A.HZXM,A.JZLX from CEN_CORE_V20.TB_YL_MZ_MEDICAL_RECORD A WHERE TO_CHAR(add_months(sysdate,-1),'yyyymm')=to_char(A.JLGXSJ,'yyyymm') ;
--select * from record4;
insert into fee
select B.jzlsh,sum(B.mxxmje),B.TFBZ from CEN_CORE_V20.TB_HIS_MZ_FEE_DETAIL B where TO_CHAR(add_months(sysdate,-1),'yyyymm')=to_char(B.stfsj,'yyyymm') and B.Mxfylb in('12','13','14')
group by B.jzlsh,B.TFBZ;
--select * from fee;
--select * from biolap.tb_yljg_sjzl_hz;
--经过处理之后的数据,经过运行之后插入到目标表
insert into biolap.tb_yljg_sjzl_hz(yljgdm,tjsj,ysgh,ysxm,ksmc,zbdm,jzlsh,kh,klx,hzxm,jzlx,mxxmje)
select A.YLJGDM,to_char(A.JLGXSJ,'yyyymm'),A.Zzysgh,A.Zzysxm,A.JZKSMC,A.Zbdm,A.Jzlsh,A.Kh,A.Klx,A.Hzxm,A.Jzlx,
sum(case when B.TFBZ='1' then B.MXXMJE when B.TFBZ='2' then -1*B.MXXMJE else 0 end ) je from record4 A,fee B
where A.JZLSH=B.JZLSH
group by a.yljgdm,to_char(A.JLGXSJ,'yyyymm'),A.Zzysgh,A.Zzysxm,A.Jzksmc,A.zbdm,A.jzlsh,A.kh,A.klx,A.Hzxm,A.Jzlx;
--删除目标表中有可能出现问题的东西(其实不应该写在这里,但是因为程序的每次查询都要用到该列,所以这么写也可以)
delete from biolap.tb_yljg_sjzl_hz where yljgdm is null;
--提交
commit;
--这种preserve类型的临时表是以会话为单位的,就算提交,临时表中的数据还会存在
--另一种delete类型的临时表,是以事务为单位的,提交之后表会仍然存在,但是表内部的数据就会清空
--为了避免混乱,直接删除临时表,避免出现更新表结构之后提示表名被占用,或者错误的沿用上一次返回的数据
drop table record4;
drop table fee;
--select * from biolap.tb_yljg_sjzl_hz where zbdm='MJZYZB';