You are on page 1of 7

set hive.groupby.orderby.position.

alias=true;
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode="nonstrict";
set hive.exec.parallel=true;
--set mapreduce.job.reduces=50;
set mapreduce.map.java.opts=-Xmx40024m;
set mapreduce.map.memory.mb=40024;
set mapreduce.reduce.java.opts=-Xmx40024m;
set mapreduce.reduce.memory.mb=40024;
--set tez.queue.name="insert_individual_policy";
--set mapred.job.queue.name="insert_individual_policy";

with individual_policy_full as (
-- LA
select
0 as policy_id,
chdrpf.chdrpfx as cont_header_prefix,
chdrpf.chdrcoy as cont_header_company,
chdrpf.validflag as valid_flag,
chdrpf.currfrom as current_from,
chdrpf.chdrnum as policy_no,
zchrpf.refnum as policy_ref_no,
--chdrpf.clntnum as customer_number,
"" as individual_id_type,
lifepf.smoking as smoker_indicator,
--cust.customer_id as customer_id,
chdrpf.cntbranch as branch_code,
chdrpf.agntnum as agent_number,
chdrpf.campaign as campaign_number,
chdrpf.cnttype as policy_type_code,
cast(nvl(covrpf.instprem ,0.0) as decimal(10,2)) as policy_premium_amount,
hpadpf.hissdte as policy_issue_date,
case
when chdrpf.billfreq == '00' then 'Single Premium'
when chdrpf.billfreq == '01' then 'Annually'
when chdrpf.billfreq == '02' then 'Semi-Annual'
when chdrpf.billfreq == '04' then 'Quarterly'
when chdrpf.billfreq == '12' then 'Monthly'
else "" end as premium_frequency,
descpf.longdesc as policy_status_desc,
chdrpf.statdate as status_change_date,
chdrpf.statreasn as status_change_reason,
chdrpf.instfrom as policy_first_premium_date,
chdrpf.instto as policy_last_premium_date,
chdrpf.ptdate as policy_next_premium_due_date,
covrpf.crtable as policy_plan,
covrpf.zextpcde as policy_ext_plan_code,
zpippf.unlineb as policy_ext_plan_name,
product.product_id as product_id,
chdrpf.payrnum as customer_role_payer_no,
chdrpf.cownnum as customer_role_owner_no,
chdrpf.despnum as customer_role_despatch_no,
chdrpf.asgnnum as customer_role_assignee_no,
"" as agent_role,
chdrpf.ccdate as policy_start_date,
chdrpf.mplnum as master_policy_id,
hpadpf.hissdte as policy_approval_date,
hpadpf.hoissdte as policy_original_date,
covrpf.risk_cess_date as policy_expiry_date,
chdrpf.crdate as policy_renew_date,
chdrpf.dtecan as policy_cancellation_date,
covrpf.risk_cess_date as policy_maturity_date,
covrpf.prem_cess_date as policy_paid_up_date,
covrpf.sumins as sum_assured,
--covrpf.coverage as coverage_no,
--covrpf.rider as rider_no,
case when life_individual.life_count=0 then 1 else life_individual.life_count end
as insured_count,
rider_individual.policy_rider_count as policy_rider_count,
"" as group_policy_indicator,
chdrpf.effdcldt as policy_reject_date,
chdrpf.zresnpd as policy_reject_reason,
chdrpf.outstamt as balance_premium_amount_lcy,
chdrpf.ptdate as policy_premium_paid_till_date,
case
when chdrpf.billchnl='C' then 'Direct Billing (Cash)'
when chdrpf.billchnl='D' then 'Direct Debit'
when chdrpf.billchnl='G' then 'Group Billing'
when chdrpf.billchnl='N' then 'No Billing'
when chdrpf.billchnl='R' then 'Credit Card'
else "" end as payment_method,
cast("" as date) as payout_divident_date,
0.0 as payout_divident_amount,
"" as benefit_return,
(case when cast(chdrpf.billfreq as int) == 0 then 1.0 else cast(chdrpf.billfreq as
double) end)*sum_instprem.sum_inst as ayfp,
--concat(pntepf.message,pntepf.seqno) as remarks,
"" as preferred_comm_channel,
case when ipd_opd_info.all_ipd > 0 then 'Y' else 'N' END
as IPD_COVERAGE_IND,
case when ipd_opd_info.all_opd > 0 then 'Y' else 'N' END
as OPD_COVERAGE_IND,
1 as source_system_id,
"Interim_DWH" as Loading_Jobs_ID,
cast(from_unixtime(unix_timestamp(),"yyyy-MM-dd") as date) as Effect_Start_Date,
cast(from_unixtime(unix_timestamp('2200-12-31', 'yyyy-MM-dd'),"yyyy-MM-dd") as
date) as Effect_End_Date
from (select * from dev_curtd_la.CHDRPF where validflag=1 ) CHDRPF
left join (select * from (select *,row_number() over (partition by chdrnum order by
smoking desc) as row_num from dev_curtd_la.LIFEPF where validflag=1) a where
a.row_num = 1) LIFEPF on (CHDRPF.CHDRNUM=LIFEPF.CHDRNUM)
left join (select * from dev_curtd_la.COVRPF where validflag<>2 and rider == '00'
and life == '01' and coverage == '01') COVRPF on (CHDRPF.CHDRNUM=COVRPF.CHDRNUM)
left join (select CHDRNUM,sum(nvl(instprem,0.0)) as sum_inst from
dev_curtd_la.COVRPF where validflag<>2
and ((statcode='IF' and (pstatcode in
('EC','ER','ET','FP','HA','PC','PP','PR','PU','SP'))) or (statcode='LR' and
pstatcode='LR'))
group by CHDRNUM ) sum_instprem on (CHDRPF.CHDRNUM=sum_instprem.chdrnum)
left join dev_curtd_la.HPADPF HPADPF on (CHDRPF.CHDRNUM=HPADPF.CHDRNUM)
left join (select DESCITEM,LONGDESC from dev_curtd_la.DESCPF where
DESCTABL='T5682') DESCPF on (CHDRPF.STATCODE=DESCPF.DESCITEM)
left join (select * from (select *,row_number() over (partition by zlsppc order by
count_result desc) as row_num from (
select *, count(*) over (partition by zlsppc,unlineb) as
count_result from dev_curtd_la.zpippf Where (unlineb != ''))a )b where row_num=1)
ZPIPPF on (COVRPF.ZEXTPCDE=ZPIPPF.ZLSPPC)
left join (select * from dev_curtd_la.ZCHRPF where VALIDFLAG=1) ZCHRPF on
(CHDRPF.CHDRNUM=ZCHRPF.CHDRNUM)
left join (select * from dev_dwh_th2.product where source_system_id = 6) product on
(COVRPF.ZEXTPCDE=product.product_plancode)
left join (select count(1) as life_count, chdrnum from dev_curtd_la.lifepf where
validflag = 1 group by chdrnum) life_individual on chdrpf.chdrnum ==
life_individual.chdrnum
left join (select count(rider) as policy_rider_count,chdrnum from
dev_curtd_la.covrpf
where covrpf.validflag<>2 and (covrpf.life != '01' or covrpf.coverage != '01' or
covrpf.rider != '00')
group by chdrnum) rider_individual on (chdrpf.chdrnum == rider_individual.chdrnum)
left join ( select tt.CHDRNUM as CHDRNUM,sum(tt.IPD_COVERAGE_IND) as
all_ipd,sum(tt.OPD_COVERAGE_IND) as all_opd
from
( select CHDRNUM,ZEXTPCDE,case when dwh_product.ipd_opd like '%IPD
%' then 1 else 0 END as IPD_COVERAGE_IND
,case when dwh_product.ipd_opd like '%OPD%' then 1 else 0 END
as OPD_COVERAGE_IND
from (select * from dev_curtd_la.COVRPF where validflag=1 )
COVRPF2
left join (select * from dev_dwh_th2.product where
source_system_id=6) dwh_product on (COVRPF2.ZEXTPCDE=dwh_product.product_plancode)
) tt
group by tt.CHDRNUM
) ipd_opd_info on (CHDRPF.CHDRNUM=ipd_opd_info.CHDRNUM)

union all
select
0 as policy_id,
"" as cont_header_prefix,
cascntrm.mcco as cont_header_company,
"" as valid_flag,
cast("" as date) as current_from,
cascntrm.mccntr as policy_no,
cascntrm.mccntr as policy_ref_no,
--cmsclntm.cmcltn as customer_number,
"" as individual_id_type,
casbene.fsnind as smoker_indicator,
--customer.customer_id as customer_id,
cascntrm.mcisst as branch_code,
casmwagt.mwagtn as agent_number,
"" as campaign_number,
cascntrm.mcctyp as policy_type_code,
cast(nvl(casbene.ftmprm,0.0) as decimal(10,2)) as policy_premium_amount,
cast(concat(lpad(cast(cascntrm.mcstly as int),4,'0'),'-',lpad(cast(cascntrm.mcstlm
as int),2,'0'),'-',lpad(cast(cascntrm.mcstld as int),2,'0')) as date) as
policy_issue_date,
case
when cascntrm.mcpmod = "A" then "Annually"
when cascntrm.mcpmod = "M" then "Monthly"
when cascntrm.mcpmod = "Q" then "Quarterly"
when cascntrm.mcpmod = "S" then "Single Premium"
end as premium_frequency,
case
when casbene.fpsflg = "C" then "Cancelled"
when casbene.fpsflg = "D" then "Death Claim"
when casbene.fpsflg = "E" then "Extended term"
when casbene.fpsflg = "I" then "Inforce"
when casbene.fpsflg = "L" then "Lapsed"
when casbene.fpsflg = "M" then "Maturity"
when casbene.fpsflg = "N" then "Not taken"
when casbene.fpsflg = "P" then "Paid-up"
when casbene.fpsflg = "R" then "Reduce paid-up"
when casbene.fpsflg = "S" then "Cash Surrender"
when casbene.fpsflg = "T" then "Terminated Due To Conversion"
when casbene.fpsflg = "V" then "Inforce vanished"
when casbene.fpsflg = "X" then "Expired"
else
"CODE Not Found"
end as policy_status_desc,
cast(concat(lpad(cast(casbene.flchgy as int),4,'0'),'-',lpad(cast(casbene.flchgm as
int),2,'0'),'-',lpad(cast(casbene.flchgd as int),2,'0')) as date) as
status_change_date,
"" as status_change_reason,
cast("" as date) as policy_first_premium_date,
cast("" as date) as policy_last_premium_date,
cast(concat(lpad(cast(cascntrm.mcpdty as int),4,'0'),'-',lpad(cast(cascntrm.mcpdtm
as int),2,'0'),'-',lpad(cast(cascntrm.mcpdtd as int),2,'0')) as date) as
policy_next_premium_due_date,
casbene.fplan as policy_plan,
casbene.fplan as policy_ext_plan_code,
pdfplan.pdesc as policy_ext_plan_name,
product.product_id as product_id,
cralph_group.cralph_pay as customer_role_payer_no,
cralph_group.cralph_owl as customer_role_owner_no,
"" as customer_role_despatch_no,
"" as customer_role_assignee_no,
"" as agent_role,
cast(concat(lpad(cast(cascntrm.mceffy as int),4,'0'),'-',lpad(cast(cascntrm.mceffm
as int),2,'0'),'-',lpad(cast(cascntrm.mceffd as int),2,'0')) as date) as
policy_start_date,
"" as master_policy_id,
cast(concat(lpad(cast(cascntrm.mcstly as int),4,'0'),'-',lpad(cast(cascntrm.mcstlm
as int),2,'0'),'-',lpad(cast(cascntrm.mcstld as int),2,'0')) as date) as
policy_approval_date,
cast(concat(lpad(cast(cascntrm.mcstly as int),4,'0'),'-',lpad(cast(cascntrm.mcstlm
as int),2,'0'),'-',lpad(cast(cascntrm.mcstld as int),2,'0')) as date) as
policy_original_date,
cast(concat(lpad(cast(casbene.fexpyy as int),4,'0'),'-',lpad(cast(casbene.fexpmm as
int),2,'0'),'-',lpad(cast(casbene.fexpdd as int),2,'0')) as date) as
policy_expiry_date,
cast("" as date) as policy_renew_date,
cast("" as date) as policy_cancellation_date,
cast(concat(lpad(cast(casbene.fexpyy as int),4,'0'),'-',lpad(cast(casbene.fexpmm as
int),2,'0'),'-',lpad(cast(casbene.fexpdd as int),2,'0')) as date)as
policy_maturity_date,
cast(concat(lpad(cast(casbene.fchgyy as int),4,'0'),'-',lpad(cast(casbene.fchgmm as
int),2,'0'),'-',lpad(cast(casbene.fchgdd as int),2,'0')) as date) as
policy_paid_up_date,
casbene.fplamt as sum_assured,
--"" as coverage_no,
--casbene.fbrcd as rider_no,
count_usrel.count_result as insured_count,
count_bene.count_result as policy_rider_count,
"" as group_policy_indicator,
cast("" as date) as policy_reject_date,
"" as policy_reject_reason,
0.0 as balance_premium_amount_lcy,
cast(concat(lpad(cast(cascntrm.mcpdty as int),4,'0'),'-',lpad(cast(cascntrm.mcpdtm
as int),2,'0'),'-',lpad(cast(cascntrm.mcpdtd as int),2,'0')) as date) as
policy_premium_paid_till_date,
case
when cascntrm.mcpycd = "BG" then "Group Billing"
when cascntrm.mcpycd = "CC" then "Credit Card"
when cascntrm.mcpycd = "DB" then "Direct Debit"
when cascntrm.mcpycd = "DD" then "Payor death"
when cascntrm.mcpycd = "DP" then "Payor disability"
when cascntrm.mcpycd = "ET" then "Electronic funds transfer"
when cascntrm.mcpycd = "FB" then "Family bill"
when cascntrm.mcpycd = "GA" then "Government allotments"
when cascntrm.mcpycd = "GR" then "Group list bill"
when cascntrm.mcpycd = "IN" then "Direct Billing (Cash)"
when cascntrm.mcpycd = "NN" then "No Billing"
when cascntrm.mcpycd = "PD" then "Pre-dated check"
when cascntrm.mcpycd = "PF" then "Premium deposit fund"
when cascntrm.mcpycd = "PY" then "Payment deduction"
when cascntrm.mcpycd = "SD" then "Sight draft"
when cascntrm.mcpycd = "SS" then "Salary savings"
else "NO PAYMENTCODE"
end as payment_method,
cast(concat(lpad(cast(casdvacm.daipty as int),4,'0'),'-',lpad(cast(casdvacm.daiptm
as int),2,'0'),'-',lpad(cast(casdvacm.daiptd as int),2,'0')) as date) as
payout_divident_date,
casdvacm.dapamt as payout_divident_amount,
"" as benefit_return,
case
when cascntrm.mcpmod = "A" then sum_bene.sum_fmdprm*1
when cascntrm.mcpmod = "M" then sum_bene.sum_fmdprm*12
when cascntrm.mcpmod = "Q" then sum_bene.sum_fmdprm*4
when cascntrm.mcpmod = "S" then sum_bene.sum_fmdprm*1
else sum_bene.sum_fmdprm*1 end as ayfp,
--"" as remarks,
"" as preferred_comm_channel,
case when ipd_opd_result.IPD_COVERAGE_IND = 1 then 'Y' else 'N' END as
ipd_coverage_ind,
case when ipd_opd_result.OPD_COVERAGE_IND = 1 then 'Y' else 'N' END as
opd_coverage_ind,
2 as source_system_id,
"Interim_DWH" as Loading_Jobs_ID,
cast(from_unixtime(unix_timestamp(),"yyyy-MM-dd") as date) as Effect_Start_Date,
cast(from_unixtime(unix_timestamp('2200-12-31', 'yyyy-MM-dd'),"yyyy-MM-dd") as
date) as Effect_End_Date
from
(select
mccntr,mcco,mcisst,mcctyp,mcstly,mcstlm,mcstld,mcpmod,mcpdtd,mcpdtm,mcpdty,mceffd,m
ceffm,mceffy,mcpycd from dev_curtd_lsp.cascntrm) cascntrm
inner JOIN
(select
fpsflg,fpolno,fsnind,ftmprm,flchgy,flchgm,flchgd,fplan,fexpdd,fexpmm,fexpyy,fchgdd,
fchgmm,fchgyy,fplamt,fbrcd,fmdprm
from dev_curtd_lsp.casbene) casbene ON CASCNTRM.MCCNTR=CASBENE.FPOLNO and
cast(fbrcd as int) = 0
left join
(select mwpoln,mwagtn
from dev_curtd_lsp.casmwagt) casmwagt on cascntrm.mccntr = casmwagt.mwpoln and
casmwagt.mwpoln = cascntrm.mccntr --3.9m
left join
(select pplan,pdesc
from dev_curtd_lsp.pdfplan) pdfplan on casbene.fplan = pdfplan.pplan --3.9m
left join
(select ipd_opd,product_plancode,product_id
from dev_dwh_th2.product_master ) product on casbene.fplan ==
product.product_plancode --3.9m
left join
(select dapoln,daiptd,daiptm,daipty,dapamt
from dev_curtd_lsp.casdvacm) casdvacm on cascntrm.mccntr = casdvacm.dapoln --3.9m
left join
(select sum(fmdprm) as sum_fmdprm,fpolno from (select fpolno,fmdprm from casbene
where fpsflg in ('E','I','V','R','P')) a group by fpolno ) sum_bene on
sum_bene.fpolno = casbene.fpolno
left join
(select count(*) as count_result from dev_curtd_lsp.casbene where casbene.fbrcd > 0
and casbene.fbrcd < 99) count_bene on 1=1
left join
(select count(crcltn) as count_result from dev_curtd_lsp.cmsusrel cmsusrel inner
join dev_curtd_lsp.casbene casbene on cmsusrel.cralph = 'INS' AND cmsusrel.crctl1 =
casbene.fpolno) count_usrel on 1=1
left join
(select crctl1,collect_set(cralph_ow1)[0] as cralph_owl,collect_set(cralph_pay)[0]
as cralph_pay from (select crctl1,case when cralph = "PAY" then crcltn end as
cralph_pay,case when cralph='OW1' then crcltn end as cralph_ow1 from
dev_curtd_lsp.cmsusrel where cast(crctl2 as int)=0 ) select_cralph group by crctl1
) cralph_group on cralph_group.crctl1 = cascntrm.mccntr
left join
(
select fpolno,max(IPD_COVERAGE_IND) as IPD_COVERAGE_IND,max(OPD_COVERAGE_IND) as
OPD_COVERAGE_IND from
(select fpolno,fplan,case when ipd_opd like '%IPD%' then 1 else 0 END as
IPD_COVERAGE_IND,case when ipd_opd like '%OPD%' then 1 else 0 END as
OPD_COVERAGE_IND
from
(select * from (select fpolno,fplan from dev_curtd_lsp.casbene) casbene left join
(select * from dev_dwh_th2.product_master ) product on
casbene.fplan=product.product_plancode
) plan_product
) select_ipd_opd group by fpolno
) ipd_opd_result on cascntrm.mccntr = ipd_opd_result.fpolno
)

Insert overwrite table dev_dwh_th2.individual_policy partition(source_system_id)


select row_number() over (order by source_system_id ASC,policy_no ASC) as
policy_individual_id,
policy_id,cont_header_prefix,cont_header_company,
valid_flag,current_from,policy_no,policy_ref_no,individual_id_type,smoker_indicator
,branch_code,
agent_number,campaign_number,policy_type_code,policy_premium_amount,policy_issue_da
te,
premium_frequency,policy_status_desc,status_change_date,status_change_reason,
policy_first_premium_date,policy_last_premium_date,policy_next_premium_due_date,
policy_plan,policy_ext_plan_code,policy_ext_plan_name,product_id,customer_role_paye
r_no,
customer_role_owner_no,customer_role_despatch_no,customer_role_assignee_no,
agent_role,policy_start_date,master_policy_id,policy_approval_date,policy_original_
date,
policy_expiry_date,policy_renew_date,policy_cancellation_date,policy_maturity_date,
policy_paid_up_date,sum_assured,insured_count,policy_rider_count,group_policy_indic
ator,
policy_reject_date,policy_reject_reason,balance_premium_amount_lcy,policy_premium_p
aid_till_date,
payment_method,payout_divident_date,payout_divident_amount,benefit_return,ayfp,
preferred_comm_channel,ipd_coverage_ind,opd_coverage_ind,
Loading_Jobs_ID, Effect_Start_Date, Effect_End_Date,source_system_id
from individual_policy_full
;

You might also like