You are on page 1of 8

// Calcular ltimos 12 meses

If p&&periodo>200112 then ( --// Calcular ltimos 12 meses

CREATE TABLE AUX_SECTOR AS SELECT*FROM P&&PERIODO_FIN

ALTER TABLE AUX_SECTOR RENAME COLUMN saldo to saldo_&&periodo_fin

ALTER TABLE AUX_SECTOR RENAME COLUMN sector to sector_&&periodo_fin

For i in reverse 12 to 0 LOOP

Pp =

* 3. Ubigeo

Create table aux_ubigeo as SELECT deudor entidad ofi_ncod tipo from

Base_deudaraw_&&periodo_fin where tipo=1 order by entidad asc, deudor asc, saldo desc;

Delete from aux_ubigeo where ofi_ncod is null oro fi_ncod=0;

***** calcular 12 meses

--* 5. Tamao

--Guardar

Create table tamao_&&periodo_fin as select deudor, entidad, tcr_ncod, saldo, cuenta, tipo

Where tipo=1 from base_deudaraw_&&periodo_fin;

//Limpieza

Delete from tamao_&&periodo_fin where entidad>=270;

Delete from tamao_&&periodo_fin where entidad=247;

Delete from tamao_&&periodo_fin where (entidad>=186 and entidad<=199);

ALTER TABLE tamao_&&periodo_fin add d_5_6 int;

UPDATE tamao_&&periodo_fin set d_5_6= floor(cuenta/100000) - floor(cuenta


/10000000)*100 ;

Delete from tamao_&&periodo_fin where d_5_6=3 or d_5_6=4 ; --// solo empresariales


--Base tipo

ALTER TABLE tamao_&&periodo_fin add tipoemp1 int;

ALTER TABLE tamao_&&periodo_fin add tipoemp2 int;

UPDATE tamao_&&periodo_fin SET tipoemp1=tcr_ncod where tcr_ncod in(16,17);

UPDATE tamao_&&periodo_fin SET tipoemp2=tcr_ncod where tcr_ncod in(19, 20);

Collapse

--*===================================

--* III. Modificaciones a base total *

--*===================================

Create table base_deuda_&&periodo_fin as select*from base_deudaraw_&&periodo_fin;

DELETE FROM base_deuda_&&periodo_fin WHERE saldo is null or saldo=0;

--*aj_cosecha

ALTER TABLE base_deuda_&&periodo_fin ADD fecha_ini int;

MERGE INTO base_deuda_&&periodo_fin U USING FECHA_INI P

ON (U.entidad= P.entidad and U.DEUDOR=P.DEUDOR)

U.FECHA_INI=P.FECHA_INI;

UPDATE base_deuda_&&periodo_fin SET fecha_ini=fecha_ini + 200000;

Alter TABLE base_deuda_&&periodo_fin ADD t int;

UPDATE base_deuda_&&periodo_fin SET t= 12*(floor(fecha/100)-floor(fecha_ini/100)) +


mod(fecha,100) - mod(fecha_ini,100); -- // Calcula la antiguedad del deudor con la entidad en
meses (t). Nota: mod(x,y) = x - y*int(x/y)

ALTER TABLE base_deuda_&&periodo_fin drop column fecha;

ALTER TABLE base_deuda_&&periodo_fin drop column fecha_ini;


--* aj_sector

ALTER TABLE base_deuda_&&periodo_fin add sector int;

Merge into base_deuda_&&periodo_fin U using aux_sector S

On U.deudor = S.deudor

Then

u.sector=s.sector

CREATE TABLE aj_sector SELECT cuenta, tcr_ncod, dig_ncod, cec_ncod, cdi_ncod, atraso,
deudor, entidad ,calent c20_ncod, ind_sobreen, rgcc_ncod, tipo, aj_fusion, t, cosecha, sector,
ofi_ncod, SUM(SALDO) FROM base_deuda_&&periodo_fin

GROUP BY cuenta, tcr_ncod, dig_ncod, cec_ncod, cdi_ncod, atraso, deudor, entidad ,calent
c20_ncod, ind_sobreen, rgcc_ncod, tipo, aj_fusion, t, cosecha, sector, ofi_ncod;

* aj_ubigeo

ALTER TABLE base_deuda_&&periodo_fin add ubigeo int;

Merge into base_deuda_&&periodo_fin U using aux_ubigeo S

On U.deudor = S.deudor

Then

u.ubigeo=s.ubigeo;

create table aj_ubigeo AS select cuenta, tcr_ncod, dig_ncod, cec_ncod ,cdi_ncod, atraso
deudor, entidad, calent ,c20_ncod ,ind_sobreen, rgcc_ncod ,tipo ,aj_fusion, t ,cosecha, sector
,ubigeo

group by cuenta, tcr_ncod, dig_ncod, cec_ncod ,cdi_ncod, atraso deudor, entidad, calent
,c20_ncod ,ind_sobreen, rgcc_ncod ,tipo ,aj_fusion, t ,cosecha, sector ,ubigeo;

* ajuste_n_ents

CREATE TABLE ajuste_n_ents as SELECT (campos de auxnents) left join on

* aj_tamano

// dgitos cuentas

Alter table base_deuda_&&periodo_fin add d_1_4 int;

Alter table base_deuda_&&periodo_fin add d_5_6 int;

Alter table base_deuda_&&periodo_fin add d_7_8 int;

Alter table base_deuda_&&periodo_fin add d_9_10 int;

// Deuda directa
Cap drop tipocod*

UPDATE base_deuda_&&periodo_fin SET d_1_4 = floor(cuenta/10000000)

UPDATE base_deuda_&&periodo_fin SET d_5_6 = floor(cuenta/100000) - floor(cuenta


/10000000)*100

UPDATE base_deuda_&&periodo_fin SET d_7_8 = floor(cuenta/1000) - floor(cuenta


/100000)*100

UPDATE base_deuda_&&periodo_fin SET d_9_10 = floor(cuenta/10) - floor(cuenta /1000)*100

Alter table base_deuda_&&periodo_fin add tipocod int;

UPDATE base_deuda_&&periodo_fin SET tipocod=1 where d_5_6<>3 and d_5_6<>4;

UPDATE base_deuda_&&periodo_fin SET tipocod=21 where d_5_6=3 and d_7_8<>2 and


tipocod is null;

UPDATE base_deuda_&&periodo_fin SET tipocod=22 where (d_5_6=3 and d_7_8=2 and


tipocod is null)or (d_7_8=6 and d_9_10=1 and tipocod=21)

UPDATE base_deuda_&&periodo_fin SET tipocod=22 where cuenta=14050319020 or


cuenta=14060319020

UPDATE base_deuda_&&periodo_fin SET tipocod=212 where tipocod=21 and d_7_8=6 and


d_9_10=2

UPDATE base_deuda_&&periodo_fin SET tipocod=213 where tipocod=21 and d_7_8=6 and


d_9_10 in (4,5,6)

UPDATE base_deuda_&&periodo_fin SET tipocod=32 where d_5_6=4 and d_7_8 in (23,24,25)


and tipocod is null;

UPDATE base_deuda_&&periodo_fin SET tipocod=31 where d_5_6=4 and tipocod<>32 and


tipocod is null;

UPDATE base_deuda_&&periodo_fin SET tipocod is null where tipo<>1;

--Contingentes

UPDATE base_deuda_&&periodo_fin SET tipocod= 41 where tipo=2 and d_7_8=3 //


consumo

UPDATE base_deuda_&&periodo_fin SET tipocod= 42 where tipo=2 and (d_7_8=13 or


d_7_8=2) // pequeas y micro

UPDATE base_deuda_&&periodo_fin SET tipocod= 43 where tipo=2 and (d_7_8=12) //


medianas

UPDATE base_deuda_&&periodo_fin SET tipocod= 44 where tipo=2 and (d_7_8>=5 and


d_7_8<=11) // grandes, corp y otros
-- Castigos --> cuenta es nica, sacarlo por tcr_ncod

UPDATE base_deuda_&&periodo_fin SET tipocod= 51 where tipo=3 and (tcr_ncod=21 or


tcr_ncod=22) // consumo

UPDATE base_deuda_&&periodo_fin SET tipocod= 52 tipo=3 and (tcr_ncod=19 or


tcr_ncod=20) // pequeas y micro

UPDATE base_deuda_&&periodo_fin SET tipocod= 53 where tipo=3 and (tcr_ncod=18) //


medianas

UPDATE base_deuda_&&periodo_fin SET tipocod= 54 where tipo=3 and (tipocod is null)


// grandes, corp y otros

UPDATE base_deuda_&&periodo_fin SET tipocod= 55 where tipo=3 and (tcr_ncod=23)

Alter table base_deuda_&&periodo_fin drop column d_1_4 ;

Alter table base_deuda_&&periodo_fin drop column d_5_6 ;

Alter table base_deuda_&&periodo_fin drop column d_7_8 ;

Alter table base_deuda_&&periodo_fin drop column d_9_10 ;

* Generar identificador nico

UPDATE base_deuda_&&periodo_fin SET dig_ncod = 1WHERE dig_ncod=3 --// VAC -->


select*from DMT_TER_DIGITO --> VAC es MN

SELECT tamao, tipocod, n_ents_* ,cuenta, tcr_ncod ,dig_ncod ,cec_ncod ,cdi_ncod ,atraso
deudor, entidad ,calent ,c20_ncod ,ind_sobreen ,rgcc_ncod ,tipo, aj_fusion, t, cosecha, sector
ubigeo, SUM(SALDO) from base_deuda_&&periodo_fin

GROUP BY tamao, tipocod, n_ents_* ,cuenta, tcr_ncod ,dig_ncod ,cec_ncod ,cdi_ncod ,atraso
deudor, entidad ,calent ,c20_ncod ,ind_sobreen ,rgcc_ncod ,tipo, aj_fusion, t, cosecha, sector
ubigeo
--LOS COLLAPSES

SELECT tamao, tipocod, n_ents_* ,cuenta, tcr_ncod, dig_ncod ,cec_ncod, cdi_ncod ,atraso
deudor, entidad, ind_sobreen ,rgcc_ncod, tipo ,aj_fusion, t ,cosecha, sector ,ubigeo,
MAX(calent), Max(c20_ncod), sum(saldo) from base_deuda_&&periodo_fin

GROUP BY tamao, tipocod, n_ents_* ,cuenta, tcr_ncod, dig_ncod ,cec_ncod, cdi_ncod ,atraso
deudor, entidad, ind_sobreen ,rgcc_ncod, tipo ,aj_fusion, t ,cosecha, sector ,ubigeo

SELECT c20_ncod ,calent, tamano ,tipocod ,n_ents_* ,cuenta, tcr_ncod ,dig_ncod ,cec_ncod
cdi_ncod, atraso , deudor, entidad, ind_sobreen, tipo, aj_fusion, t ,cosecha, sector, ubigeo
MAX(Rgcc_ncod), sum(saldo) from base_deuda_&&periodo_fin

GROUP BY c20_ncod ,calent, tamano ,tipocod ,n_ents_* ,cuenta, tcr_ncod ,dig_ncod


,cec_ncod cdi_ncod, atraso , deudor, entidad, ind_sobreen, tipo, aj_fusion, t ,cosecha, sector,
ubigeo

SELECT tamao, tipocod, n_ents_*, cuenta, tcr_ncod, dig_ncod ,cec_ncod ,cdi_ncod


atraso , deudor, entidad, ind_sobreen ,tipo, aj_fusion, t, cosecha, sector, ubigeo
,MAX(Rgcc_ncod), MAX(c20_ncod), MAX(calent), sum(saldo) from
base_deuda_&&periodo_fin

GROUP BY SELECT tamao, tipocod, n_ents_*, cuenta, tcr_ncod, dig_ncod ,cec_ncod ,cdi_ncod
atraso , deudor, entidad, ind_sobreen ,tipo, aj_fusion, t, cosecha, sector, ubigeo

--* Hacer variable con saldo en moneda

ALTER TABLE base_deuda_&&periodo_fin ADD SALDO_ME INT;

UPDATE base_deuda_&&periodo_fin SET SALDO_ME=SALDO WHERE DIG_NCOD=2;

ALTER TABLE base_deuda_&&periodo_fin ADD SALDO_SOLES INT;

UPDATE base_deuda_&&periodo_fin SET SALDO_SOLES=SALDO WHERE DIG_NCOD=1;

UPDATE base_deuda_&&periodo_fin SET SALDO_SOLES=0 WHERE DIG_NCOD<>1;

UPDATE base_deuda_&&periodo_fin SET SALDO_ME=0 WHERE DIG_NCOD<>2;

SELECT rgcc_ncod, c20_ncod, calent, tamano ,tipocod, n_ents_* ,cuenta, tcr_ncod, cec_ncod
cdi_ncod ,atraso , deudor ,entidad, ind_sobreen, tipo, aj_fusion ,t, cosecha, sector, ubigeo,
SUM(SALDO), SUM (SALDO_SOLES), SUM (SALDO_ME)

GROUP BY rgcc_ncod, c20_ncod, calent, tamano ,tipocod, n_ents_* ,cuenta, tcr_ncod,


cec_ncod cdi_ncod ,atraso , deudor ,entidad, ind_sobreen, tipo, aj_fusion ,t, cosecha, sector,
ubigeo

--// reportar como error


* Saldo castigado // aadirlo a la derecha para evitar errores operativos

Preserve

CREAR TABLE CASTIGOS AS SELECT * FROM base_deuda_&&periodo_fin;

DELETE FROM CASTIGOS WHERE TIPO <>3;

ALTER TABLE CASTIGOS RENAME COLUMN SALDO TO SALDO_Castigos;

ALTER TABLE CASTIGOS RENAME COLUMN ATRASO TO ATRASO_Castigos;

Create table castigos1 as SELECT tamano ,tcr_ncod ,cec_ncod, cdi_ncod ,deudor ,entidad
,aj_fusion ,t ,cosecha, sector ubigeo , SUM (atraso_castigos) FROM castigos

DELETE FROM base_deuda_&&periodo_fin Where tipo =3

MERGE INTO

You might also like