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(saldo_amt) MTO_SALDO,
                                     MAX(flg_lin_mercado) flg_lin_mercado, 
                                     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 cod_sbs_val in (select cod_sbs_val from .....................t_vpc_segmentacion_merc where banca_fin_val = 'BPE' or banca_mes_val = 'BPE')
                               AND tipo_producto_rcc_desc IN (
                                    'COL. DIRECTAS','REACTIVA','COL. INDIRECTAS','FAE','INMOBILIARIO'
                               )
                               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')
                              )
                              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