DELETE FROM DLAB_MODELOS.TMP_JTT_SEG_BPE_CTA_NEG;
INSERT INTO DLAB_MODELOS.TMP_JTT_SEG_BPE_CTA_NEG
WITH MAXIMA_FECHA_SECTORISTA as(
    SELECT MAX(FecInformacion_Dt) max_fec_del_mes
 FROM  E_DW_VIEWS.V_VPC_EQUIV_SECTORISTA_HST 
 WHERE CAST(CAST(FecInformacion_Dt AS FORMAT 'YYYYMM') AS VARCHAR(6)) = (SELECT MAX(PERIODO) FROM DLAB_MODELOS.HM_LAST_MONTH)
),
--SELECT MAX(max_fec_del_mes) FROM MAXIMA_FECHA_SECTORISTA
MAX_FEC_COL_SEG_DIA as(
    SELECT MAX(Fecha_Informacion_Dt) max_fec_del_mes,
       CAST((CAST(max(Fecha_Informacion_Dt) AS FORMAT 'YYYYMM')) AS CHAR(6)) || '01' min_fec_del_mes
 FROM  E_DW_VIEWS.V_MST_SEGUIMIENTO_SALD_DIA_VPC 
 WHERE Periodo_Val = (SELECT MAX(PERIODO) FROM DLAB_MODELOS.HM_LAST_MONTH)
)
--SELECT MAX(max_fec_del_mes), MIN(min_fec_del_mes) FROM MAX_FEC_COL_SEG_DIA
SELECT b.Periodo_Val,
    b.Cod_Unico_Val,
    b.Nro_Cuenta_Producto_Val, 
    b.Fecha_Apertura_Dt,
    b.Categoria_Prod_Vpc_Dsc,
  c.CodigoRegistro_Cd,
  'C' AS SEGMENTO,
  '3-Pasivero/Potencial' AS SEGMENTO_CICS
from E_DW_VIEWS.V_MST_SEGUIMIENTO_SALD_DIA_VPC b
left join E_DW_VIEWS.V_VPC_EQUIV_SECTORISTA_HST c
 on c.FecInformacion_Dt = (SELECT MAX(max_fec_del_mes) FROM MAXIMA_FECHA_SECTORISTA)
 and c.CodigoSectorLargo_Cd = b.Cod_Sectorista_Cd
where 1 = 1
and b.Fecha_Informacion_Dt = (SELECT MAX(max_fec_del_mes) FROM MAX_FEC_COL_SEG_DIA)  -- --TO_DATE('20230530','YYYYMMDD'
and b.Categoria_Prod_VPC_Dsc like '%NEGOCIO%'
and b.Fecha_Apertura_Orig_Val >= (sel MIN(min_fec_del_mes) from MAX_FEC_COL_SEG_DIA)

--) WITH DATA PRIMARY INDEX (Periodo_Val, Cod_Unico_Val);

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