My variable is not in the group - Error

Asked

Viewed 15 times

0

I am running a query but is giving the error. In Databricks is appearing the following message:

"Analysisexception: Expression 'Aux1.nom_canal_escritorio' is neither present in the group by, nor is it an Aggregate Function. Add to group by or wrap in first() (or first_value) if you don’t care which value you get.;"

This is my code:

df_captacao_novos_clientes = spark.sql("""
      with aux1 as (
          select    dt.num_ano_mes                as num_ano_mes
                  , dt.num_ano                    as num_ano
                  , dt.nom_mes_abreviado          as nom_mes_abreviado
                  , dc.cod_dim_conta              as cod_dim_conta
                  , case 
                      when dce.nom_canal_n6_vigente != 'Escritórios' 
                      then 'B2C' 
                      else 'B2B' 
                    end                           as canal
                  , case 
                      when dce.nom_canal_n6_vigente == 'Escritórios'
                      then dce.cod_matriz
                      else dce.nom_canal_n6_vigente                     
                    end                           as nom_canal_escritorio
                  , fc.val_movimento              as valor
                  , dc.cod_conta                  as cod_conta

          from    layer3.fat_captacao fc
          --
          join    layer3.dim_tempo dt
          on      dt.cod_dim_tempo = fc.cod_dim_tempo
          --
          join    layer3.dim_canal_escritorio dce
          on      dce.cod_dim_canal_escritorio = fc.cod_dim_canal_escritorio
          --
          join    layer3.dim_conta dc 
          on      dc.cod_dim_conta = fc.cod_dim_conta
          --
          join    layer3.dim_faixa_aporte dfa
          on      dfa.cod_dim_faixa_aporte = fc.cod_dim_faixa_aporte
          and     dfa.cod_faixa_aporte = 1
          --
          where   dce.nom_canal_n6_vigente in ({0})
          and     dt.num_ano_mes >= {1}
          and     dt.num_ano_mes <= {2}
      )
      --
      select    '2.0 Captação Total'                        as alavanca_1
              , '2.1 Captação novos clientes'               as alavanca_2
              , '2.1 Captação novos clientes'               as alavanca_3
              , aux1.num_ano_mes                            as num_ano_mes
              , aux1.num_ano                                as num_ano
              , aux1.nom_canal_escritorio                   as escritorio
              , 'R$'                                        as unidade
              , sum(aux1.valor)                             as valor
              , count(distinct cod_conta)
      --
      from    aux1
      --
      group   by aux1.num_ano_mes
              , aux1.num_ano
              , aux1.canal
      --
      order   by aux1.canal
              , aux1.num_ano_mes
""".format(lista_canais, data_inicio, data_fim))
display(df_captacao_novos_clientes)

Can someone please help me? I don’t know what else to do!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.