Está en la página 1de 15

set @PaymentStartDate= '2017-09-01';

set @PaymentENdDate = '2017-09-06';

###RUN THE FIRST TEMPDB PARTS EVERY SINGLE DAY FOR THE CURRENT MONTH of payments####

###Payment month will be one month only###

#####

Drop temporary table if exists tempdb.connected;

Create temporary table tempdb.connected

Select distinct concat(CPF_CNPJ,Oldest_outstanding_installment_Number_OTH1)


CPF_CNPJ,min(Calltime)Calltime from `bv_toledo.afedata` where campaign = 'CP_BV_05' and
date(calltime)>=@PaymentStartDate group by 1;

Alter table tempdb.connected add index index01 (CPF_CNPJ);

Alter table tempdb.connected add index index02 (Calltime);

##New leads all because dates selected later based on filename + implemented logic

Drop temporary table if exists tempdb.newlead;

Create temporary table tempdb.newlead

Select *,concat(CPF,Oldest_outstanding_installment_number) CPF_CNPJ from


bvsatmap.`bv_toledo.newleads`;

Alter table tempdb.newlead add index index02 (CPF_CNPJ);

Alter table tempdb.newlead add index index03 (Startdate);

Drop temporary table if exists tempdb.newleads22;

Create temporary table tempdb.newleads22


Select a.* from tempdb.newlead a inner join tempdb.connected b on a.CPF_CNPJ = b.CPF_CNPJ

###RUN THE FIRST TEMPDB PARTS EVERY SINGLE DAY FOR THE CURRENT MONTH of payments####

###Payment month will be one month only###

#####

set @PaymentStartDate= '2017-09-01';

set @PaymentENdDate = '2017-09-06';

##New leads all because dates selected later based on filename + implemented logic

DROP TEMPORARY TABLE IF EXISTS tempdb.new_leads_all;

Create temporary table if not exists tempdb.new_leads_all

select distinct Lead_sent_Date Date_1,filename,

cast(substr(filename,1,instr(filename,'_')-1) as date) file_date,

cpf,My_ContractNumber,

Oldest_outstanding_installment_number,due_Date,

trim(leading '0' from CPF) as new_CPF ,

Description_of_region_code, region_code,

TRIM(LEADING '0' FROM region_code) region_code_1,

(case when on_off = 'true' then 1 else 0 end) as ON_off_1

from tempdb.newleads22

where filename like '%consolidado_186%'

and my_contractnumber not in


('12067000105991','12261000007113','12168000050232','12040000165905') ;
###Logic for filename date to exclude weekends and have a lead on weekday only

DROP TEMPORARY TABLE IF EXISTS tempdb.new_leads_all2;

Create temporary table if not exists tempdb.new_leads_all2

select *,date(if(weekday(file_date)=5,date_add(file_date, Interval 2 day),date_add(file_date, interval 1


day))) as Lead_Sent_date

from tempdb.new_leads_all;

DROP TEMPORARY TABLE IF EXISTS tempdb.new_leads;

Create temporary table if not exists tempdb.new_leads

Select * from tempdb.new_leads_all2

WHERE month(Lead_Sent_date) >= month(@PaymentStartDate)-2 and


month(Lead_Sent_date)<month(@PaymentStartDate)+1

and date(Lead_Sent_date)<curdate()-2; ##we report only on dates we have payments for

##and month(due_date)>= @PaymentMonth-3 and year(due_date)>= '2017'; # if administrative


separate

alter table tempdb.new_leads add index `index001` (`new_CPF`);

-- Taking specific leads from 05 regions of CON and NE only

DROP TEMPORARY TABLE IF EXISTS tempdb.new_leads1;

Create temporary table if not exists tempdb.new_leads1

select distinct *,

(CASE WHEN region_code_1 in


('102','118','119','120','200','267','268','269','271','318','34','87','206','227',

'244','362','15','65','83','153','175','188','254','317','53','140','275','363','52','112','201','276','101','129','1
30','270','272','129','128','177','222','72','131',
'208','289','85','124','156','250','364','45','86','243','342','343','372','84','158','209','283','305','306','309','
66','71','127','139','212','247','287','344','345',

'346','347','371','70','159','213','282','332','333','94','171','245','195','219','288')

THEN 'Inscope' else 'NA' END ) InScope_Campaign

from tempdb.new_leads

order by My_contractnumber, Lead_Sent_date asc;

-- Generating RowNo

Set @RowNo := 0;

DROP TEMPORARY TABLE IF EXISTS tempdb.newleads_rownum1;

Create temporary table if not exists tempdb.newleads_rownum1

SELECT *, @RowNo :=@RowNo+1 LeadID

FROM tempdb.new_leads1;

DROP TEMPORARY TABLE IF EXISTS tempdb.newleads_rownum2;

Create temporary table if not exists tempdb.newleads_rownum2

SELECT * FROM tempdb.newleads_rownum1

ORDER BY My_contractnumber, Lead_sent_date;

alter table tempdb.newleads_rownum1 add index `index1` (`My_contractnumber`,`LeadID`);

alter table tempdb.newleads_rownum2 add index `index2` (`My_contractnumber`,`LeadID`);

drop temporary table if exists tempDB.newleads_2;

Create temporary table if not exists tempDB.newleads_2 As

select a.*,
ifnull(b.Lead_Sent_Date,curdate()) as Next_Lead_Sent_Date

from tempDB.newleads_rownum1 a

left join tempDB.newleads_rownum2 b

on a.My_contractnumber = b.My_contractnumber

And a.LeadID + 1 = b.LeadID

order by My_contractnumber;

alter table tempdb.newleads_2 add index `index3`


(`My_contractnumber`,`Lead_Sent_Date`,`Next_Lead_Sent_Date`);

##same day leads and earlier payments cases to exclude

drop temporary table if exists tempdb.d0;

create temporary table if not exists tempdb.d0

select a.*,DATE(LEFT(FILE,8)) AS D0FILEDATE, cast(PGDMACCT as char) d0leads from


stagingdb.`remessa_d0` a

where month(date) >= month(@PaymentStartDate)-2 and month(date) < month(@PaymentStartDate)


+1;

-- is there bias?

alter table tempdb.d0 add index contractleaddt1 (D0FILEDATE,d0leads);

DROP TEMPORARY TABLE IF EXISTS TEMPDB.newleads3;

CREATE TEMPORARY TABLE IF NOT EXISTS TEMPDB.newleads3

SELECT a.*,b.d0leads FROM TEMPDB.newleads_2 A

left JOIN TEMPDB.D0 B

ON B.d0leads = A.my_contractnumber

AND b.D0FILEDATE = a.file_date;


DROP TEMPORARY TABLE IF EXISTS TEMPDB.newleads4;

CREATE TEMPORARY TABLE IF NOT EXISTS TEMPDB.newleads4

SELECT a.* FROM TEMPDB.newleads3 A

where d0leads is null;

#same day leads and same day payments cases

drop temporary table if exists tempdb.exclusao;

create temporary table if not exists tempdb.exclusao

select DATE(LEFT(FILE,8)) AS exFILEDATE,a.*

from bvsatmap.tol_remessa_exclusao_retirada_21 a

where month(date) >=month(@PaymentStartDate)-2 and date< date_add(@PaymentStartDate, interval


32 day);

alter table tempdb.exclusao add index excl (numero_do_contrato,exFILEDATE);

DROP TEMPORARY TABLE IF EXISTS TEMPDB.newleads5;

CREATE TEMPORARY TABLE IF NOT EXISTS TEMPDB.newleads5

SELECT a.*,b.numero_do_contrato as CNexclu, b.file FROM TEMPDB.newleads4 A

left JOIN

TEMPDB.exclusao B

ON B.numero_do_contrato = A.my_contractnumber

AND b.exFILEDATE = a.lead_sent_date;

drop temporary table if exists tempdb.newleads6;

create temporary table if not exists tempdb.newleads6


select * from tempdb.newleads5

where cnexclu is null;

-- two solutions-- add a filter in the join that removes these cases.

-- join on contract # and invoice. figure out which the contract

#####ALL PAYMENTS####

DROP TEMPORARY TABLE IF EXISTS tempdb.PaymentsData_1;

Create temporary table if not exists tempdb.PaymentsData_1

SELECT NR_BLT as Toledo_Invoice_Code,

CD_CLI as Internal_code_for_phase,

NR_CNPJ_CPF as CPF,

CD_CTR as ContractNumber,

NR_PCL as InstallmentNumber,

DT_EMS_RCB as ReceiptDate,

DT_PGT_RCB as Invoice_Payment_Date,

VR_TOT_PGT as Total_Amount,

'NA' AS Bank_Invoice_Code

FROM bvsatmap.`bv_toledo.pagamentos`

where (NR_CNPJ_CPF is not null or NR_CNPJ_CPF <>'')

AND ID_SIT_RCB <>'C' ###not a cancelled payment/invoice

AND month(DT_PGT_RCB) = month(@PaymentStartDate)

and date(DT_PGT_RCB)<'2017-06-01' and date(DT_PGT_RCB)>'2017-05-01' ;

DROP TEMPORARY TABLE IF EXISTS tempdb.PaymentsData_part1;


Create temporary table if not exists tempdb.PaymentsData_part1

SELECT Toledo_Invoice_Code Invoice_code,

count(*) Number_Payments,

MAX(CPF) CPF, MAX(ContractNumber) ContractNumber,

group_concat(InstallmentNumber SEPARATOR ' | ') InstallmentNumber,

MAX(ReceiptDate) ReceiptDate, MAX(Invoice_Payment_Date) Invoice_Payment_Date,

SUM(Total_Amount) Total_Amount

FROM tempdb.PaymentsData_1

GROUP BY Toledo_Invoice_Code; ##group on bank invoice code for new payments format

##FOR NEW PAYMENTS LATER

DROP TEMPORARY TABLE IF EXISTS tempdb.PaymentsData_2;

Create temporary table if not exists tempdb.PaymentsData_2

SELECT 'NA' as Toledo_Invoice_Code,

CODE as Internal_code_for_phase,

CPF as CPF,

CONTRATOGESTAO as ContractNumber,

PAR as InstallmentNumber,

DT_STATUS as ReceiptDate,

DT_PAGAMENTO as Invoice_Payment_Date,

-- ID_SIT_RCB as Invoice_Status,

VR_PAGAMENTO as Total_Amount,

TPNOSSO_NUMERO AS Bank_Invoice_Code

FROM bvsatmap.`bv_toledo.payments_new`

where date(DT_PAGAMENTO)>='2017-06-01'
and month(DT_Pagamento) = month(@paymentstartdate)

and date(DT_PAGAMENTO) >= @PaymentStartDate and date(DT_PAGAMENTO) < @Paymentenddate;

DROP TEMPORARY TABLE IF EXISTS tempdb.PaymentsData_part2;

Create temporary table if not exists tempdb.PaymentsData_part2

SELECT Bank_Invoice_Code Invoice_code,

count(*) Number_Payments,

MAX(CPF) CPF, MAX(ContractNumber) ContractNumber,

group_concat(InstallmentNumber SEPARATOR ' | ') InstallmentNumber,

MAX(ReceiptDate) ReceiptDate, MAX(Invoice_Payment_Date) Invoice_Payment_Date,

SUM(Total_Amount) Total_Amount

FROM tempdb.PaymentsData_2 lm

GROUP BY Bank_Invoice_Code;

##Only one payments table for the rest of the query

DROP TEMPORARY TABLE IF EXISTS tempdb.PaymentsData_all;

Create temporary table if not exists tempdb.PaymentsData_all

select * from tempdb.PaymentsData_part1

UNION

select * from tempdb.PaymentsData_part2;

DROP TEMPORARY TABLE IF EXISTS tempdb.PaymentsData;

Create temporary table if not exists tempdb.PaymentsData


select *,left(InstallmentNumber,3) Lowest_Installment_cleared from tempdb.PaymentsData_all;

Select lowest_installment_cleared from tempdb.paymentsdata ;

alter table tempdb.PaymentsData add index `index6` (`ContractNumber`,`Invoice_Payment_Date`);

drop temporary table if exists tempDB.Report;

Create temporary table if not exists tempDB.Report As

SELECT LL.*,PP.Invoice_code, PP.CPF Payments_CPF, PP.ContractNumber,PP.InstallmentNumber,


PP.Invoice_Payment_Date,PP.Total_Amount,

Lowest_Installment_cleared

FROM tempdb.newleads6 LL

LEFT JOIN tempdb.PaymentsData PP

ON LL.My_ContractNumber =PP.ContractNumber

and PP.Invoice_Payment_Date>=Date(LL.Lead_Sent_Date) and PP.Invoice_Payment_Date < LL.


Next_Lead_Sent_Date

and (datediff(invoice_payment_date,due_date)<=70) and LL.Oldest_outstanding_installment_number =


PP.Lowest_Installment_cleared

order by My_ContractNumber;

Drop temporary table if exists tempdb.trouble;

Create temporary table tempdb.trouble

Select * from tempdb.report where Payments_CPF is not null;

##add payment month and lead mont


drop temporary table if exists tempDB.Report2;

Create temporary table if not exists tempDB.Report2

Select *, month(Invoice_Payment_Date) Payment_Month, month(Lead_Sent_Date) Lead_Sent_Month,

month(@paymentstartdate) Report_Month,'2017' Report_Year

from tempDB.Report ;

Select distinct char_length(My_ContractNumber) from tempdb.Report2;

###custom report table we will draw on for all

drop temporary table if exists tempDB.Custom_Report;

Create temporary table if not exists tempDB.Custom_Report As

SELECT Report_Month, Report_Year,Payment_Month,Lead_Sent_Month,

Lead_Sent_Date, Invoice_payment_date,

Count(distinct case when ON_off_1 = 1 then My_ContractNumber else NULL end )


ON_CONTRACTS_in_Leads,

Count(distinct case when ON_off_1 = 0 then My_ContractNumber else NULL end )


OFF_CONTRACTS_in_Leads,

SUM(CASE WHEN (ON_OFF_1 = 1 and Invoice_Payment_Date is not NULL ) THEN 1 ELSE 0 END)
ON_Payments_made,

SUM(CASE WHEN ( ON_OFF_1 = 0 and Invoice_Payment_Date is not NULL ) THEN 1 ELSE 0 END)
OFF_Payments_made,

STDDEV(CASE WHEN (ON_OFF_1 = 1 and Invoice_Payment_Date is not NULL ) THEN 1 ELSE 0 END)
ON_Stddev,

STDDEV(CASE WHEN ( ON_OFF_1 = 0 and Invoice_Payment_Date is not NULL ) THEN 1 ELSE 0 END)
OFF_Stddev

FROM tempDB.Report2

where Lead_Sent_Date is not NULL and on_off_1 is not null and My_ContractNumber is not null

GROUP BY Lead_Sent_Date, invoice_payment_date


ORDER BY Lead_Sent_Date, invoice_payment_date;

Select * from tempdb.custom_report;

###DELETE EVERYTHING IN CUSTOM REPORT FOR CURRENT PAYMENT/ REPORT_MONTH#### EXAMPLE


DELETE EVERYTHING FOR REPORT_MONTH=JULY TODAY!

-- DELETE FROM tempDB.Custom_Report

-- where Report_Month= ###MONTH UNDER CONSIDERATAION

#Insert into tempDB.Custom_Report

###PORTAL QUERYING STARTS


HERE#################################################################################
##########################

##CLIENT VIEW

-- some one inputs payment month!

set @PaymentMonth= 9;

###portal queries the table

drop temporary table if exists tempDB.ClientPortal_Report;

Create temporary table if not exists tempDB.ClientPortal_Report As

SELECT max(Payment_Month) Payment_Month,Month(lead_sent_date) lead_sent_month,

sum(ON_CONTRACTS_in_Leads) ON_Leads,sum(OFF_CONTRACTS_in_Leads) OFF_Leads,

sum(ON_Payments_made) ON_Payments, sum(OFF_Payments_made) OFF_Payments,


cast((sum(ON_Payments_made) /sum(ON_CONTRACTS_in_Leads)) as decimal (19,8)) ON_CR,

cast((sum(OFF_Payments_made)/sum(OFF_CONTRACTS_in_Leads)) as decimal (19,8)) OFF_CR

FROM tempDB.Custom_Report

where Report_month = @PaymentMonth

group by Month(lead_sent_date);

###Show on CLIENT portal

Drop temporary table if exists tempdb.gain;

Create temporary table tempdb.gain

SELECT Payment_Month, lead_sent_month, ON_Leads, OFF_Leads, ON_Payments, OFF_Payments,

100*(ON_CR) as ON_CR, 100*(OFF_CR) OFF_CR,

100*(ON_CR-OFF_CR) Lift, (ON_CR-OFF_CR)*ON_Leads AS Incrementals,

100*((ON_CR-OFF_CR)*ON_Leads)/(ON_Payments-((ON_CR-OFF_CR)*ON_Leads)) Gain

from tempDB.ClientPortal_Report;

Select (((sum(on_payments)/sum(On_leads))/(sum(off_payments)/sum(off_leads))) - 1)*100 from


tempdb.gain;

##INTERNAL VIEW

-- some one inputs two dates in one month!

set @startinterval = '2017-09-01';

set @Endinterval = '2017-09-06';

### temp table for internal report ########

drop temporary table if exists tempDB.InternalPortal_Report;

Create temporary table if not exists tempDB.InternalPortal_Report As


select lead_sent_date, month(@startinterval) Payment_Month,

sum(ON_Contracts_In_Leads) as ON_CONTRACTS_in_Leads,

sum(OFF_Contracts_In_Leads) as OFF_CONTRACTS_in_Leads,

sum(case when invoice_payment_date >= @startinterval and invoice_payment_date < @endinterval


then ON_Payments_made else 0 end) as ON_Payments_made,

sum(case when invoice_payment_date >= @startinterval and invoice_payment_date < @endinterval


then OFF_Payments_made else 0 end) as OFF_Payments_made

from tempdb.custom_report

where Report_month = month(@startinterval)

group by lead_sent_date

order by lead_sent_date;

##Step2 for internal

drop temporary table if exists tempDB.InternalPortal_Report2;

Create temporary table if not exists tempDB.InternalPortal_Report2 As

SELECT max(Payment_Month) Payment_Month,lead_sent_date,

sum(ON_CONTRACTS_in_Leads) ON_Leads,sum(OFF_CONTRACTS_in_Leads) OFF_Leads,

sum(ON_Payments_made) ON_Payments, sum(OFF_Payments_made) OFF_Payments,

cast((sum(ON_Payments_made) /sum(ON_CONTRACTS_in_Leads)) as decimal (19,8)) ON_CR,

cast((sum(OFF_Payments_made)/sum(OFF_CONTRACTS_in_Leads)) as decimal (19,8)) OFF_CR

FROM tempDB.InternalPortal_Report

group by lead_sent_date;

###Show on internal portal

SELECT Payment_Month, lead_sent_date, ON_Leads, OFF_Leads, ON_Payments, OFF_Payments,

100*(ON_CR) ON_CR, 100*(OFF_CR) OFF_CR,


100*(ON_CR-OFF_CR) Lift, (ON_CR-OFF_CR)*ON_Leads AS Incrementals,

100*((ON_CR-OFF_CR)*ON_Leads)/(ON_Payments-((ON_CR-OFF_CR)*ON_Leads)) Gain

from tempDB.InternalPortal_Report2;

También podría gustarte