DROP TABLE IF EXISTS ..............T_MERCADO_BPE_ADQ_SF_P01


				CREATE TABLE ..............T_MERCADO_BPE_ADQ_SF_P01
				WITH ( format = 'Parquet', 
					   parquet_compression = 'SNAPPY', 
					   partitioned_by = ARRAY['PERIODO'], 
					   external_location= 's3://................................................./T_MERCADO_BPE_ADQ_SF_P01/'
					 )
				AS (
				SELECT 
					  cod_sbs_val AS CODSBS,
					  producto_rcc_desc AS PRODUCTO,
					  situacion_rcc_desc AS SITUACION,
					  empresa_cd AS COD_EMPRESA,
					  CASE WHEN coalesce(tipoempresafinanc_desc,'') = 'BANCOS' AND coalesce(estadoempresafinanc_desc,'') ='EN OPERACION' THEN 'BANCOS' ELSE 'CAJAS & FINANC.' END AS TIPO_ENTIDAD,
					  CASE WHEN coalesce(tipoempresafinanc_desc,'') = 'BANCOS' AND coalesce(estadoempresafinanc_desc,'') ='EN OPERACION' THEN nomcortoempresafinanc_desc ELSE 'CAJAS & FINANC.' END AS BANCO,
					  tipo_producto_rcc_desc TIPO_PRODUCTO,
					  SUM(coalesce(saldo_amt, 0)) MTO_SALDO,
				      MAX(coalesce(flg_lin_mercado, 'N')) flg_lin_mercado,
					  MAX(coalesce(condicion_val, 0)) MAX_DIAS_ATRASO,
                      CASE WHEN cod_sbs_val in (select cod_sbs_val from ................t_vpc_segmentacion_merc where banca_fin_val = 'BPE' or banca_mes_val = 'BPE') THEN 1 ELSE 0 END FLG_BPE_TIMELINE,
                      CASE WHEN cod_sbs_val in (select cod_sbs_val from ................t_vpc_segmentacion_merc where banca_fin_val = 'BE' or banca_mes_val = 'BE') THEN 1 ELSE 0 END FLG_BE_TIMELINE,
                      CASE WHEN cod_sbs_val in (select cod_sbs_val from ................t_vpc_segmentacion_merc where banca_fin_val = 'BC' or banca_mes_val = 'BC') THEN 1 ELSE 0 END FLG_BC_TIMELINE,
                      CASE WHEN cod_sbs_val in (select cod_sbs_val from ................t_vpc_segmentacion_merc where banca_fin_val = 'MICRO' or banca_mes_val = 'MICRO') THEN 1 ELSE 0 END FLG_MICRO_TIMELINE,
					  periodo_val AS PERIODO
				   FROM ................T_VPC_FACT_SALDOS_RCC
				   WHERE 1 = 1
				   AND date_parse(cast(periodo_val as varchar),'%Y%m') >= (SELECT date_add('month', -18, date_parse(cast(MAX(periodo_val) as varchar),'%Y%m')) FROM ................T_VPC_FACT_SALDOS_RCC)
				   AND tipo_producto_rcc_desc IN (
					 'COL. DIRECTAS','REACTIVA','COL. INDIRECTAS','FAE','INMOBILIARIO', 'CREDITOS CASTIGADOS', 'GARANTIA_FAE', 'GARANTIA_REACTIVA', 'GARANTIAS', 'REPROGRAMADOS'
				   )
				   AND 
				   (
					  (
						  flg_lin_mercado = 'S' AND
						  --(
							producto_rcc_desc NOT IN ('OTROS') 
							--OR 
							--(
							--  B.PRODUCTO ='OTROS' AND 
							--  B.DETALLE IN ('FINANCIACION DE PROYECTOS')
							--)
						  --)
					  )					
					  OR tipo_producto_rcc_desc IN ('REACTIVA','FAE','INMOBILIARIO', 'CREDITOS CASTIGADOS', 'GARANTIA_FAE', 'GARANTIA_REACTIVA', 'GARANTIAS', 'REPROGRAMADOS')
				  )
				  GROUP BY 
					  periodo_val,
					  cod_sbs_val,
					  producto_rcc_desc,
					  situacion_rcc_desc,
					  empresa_cd,
					  CASE WHEN coalesce(tipoempresafinanc_desc,'') ='BANCOS' AND coalesce(estadoempresafinanc_desc,'') ='EN OPERACION' THEN 'BANCOS' ELSE 'CAJAS & FINANC.' END,
					  CASE WHEN coalesce(tipoempresafinanc_desc,'') ='BANCOS' AND coalesce(estadoempresafinanc_desc,'') ='EN OPERACION' THEN nomcortoempresafinanc_desc ELSE 'CAJAS & FINANC.' END,
					  tipo_producto_rcc_desc
				  
				)

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