DELETE FROM DLAB_MODELOS.TMP_JTT_SEG_BPE_COL_NETA;
INSERT INTO DLAB_MODELOS.TMP_JTT_SEG_BPE_COL_NETA
WITH MAX_FEC_COL_NETA as(
    SELECT MAX(FecInformacion_Dt) max_fec_del_mes
 FROM  E_DW_VIEWS.V_FACT_VPC_COLNETAS 
 WHERE Periodo_Desemb_Val = (SELECT MAX(PERIODO) FROM DLAB_MODELOS.HM_LAST_MONTH)
)
SELECT a.Periodo_Val,
   a.Cod_Unico_Val,
   CAST( a.Credito_Cd AS CHAR(50)) Credito_Cd, 
   a.Fecha_Desemb_Dt,
   a.Producto_Real_Val,
   a.RegEjecutivo_Val,
   (CASE WHEN E.REGISTRO IS NOT NULL THEN 'A' ELSE 'B' END) AS SEGMENTO,
   (CASE WHEN E.REGISTRO IS NOT NULL THEN '1-Priority' ELSE '3-Pasivero/Potencial' END) AS SEGMENTO_CICS
FROM E_DW_VIEWS.V_FACT_VPC_COLNETAS a
LEFT JOIN(
    SELECT * --COUNT(1), COUNT(DISTINCT(REGISTRO)) 
 FROM DLAB_MODELOS.HM_BPE_PLANTA_EJECUTIVOS 
 WHERE ESTADO IN ('TENDRÁ META')
 AND PERIODO = (SELECT MAX(PERIODO) FROM DLAB_MODELOS.HM_BPE_PLANTA_EJECUTIVOS)
) E 
ON a.RegEjecutivo_Val = E.REGISTRO 
where a.FecInformacion_Dt = (sel MAX(max_fec_del_mes) from MAX_FEC_COL_NETA)
and a.Centro_Val <> &#039;RECUPERACIONES&#039;
AND a.Producto_Real_Val IN (&#039;CAPITAL DE TRABAJO&#039;,&#039;LINEA REVOLVENTE&#039;)
--) WITH NO DATA PRIMARY INDEX (Periodo_Val, Cod_Unico_Val);

Add a code snippet to your website: www.paste.org