Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

sql - ORA-00937:não é uma função de grupo de grupo único


Eu recomendo grouping sets :
select coalesce(location, 'Total') as location,
       coalesce(home_team_name, 'Total') as home_team_name,
       sum(case when match_date >= date '2018-01-01' and 
                     match_date < date '2018-02-01'
                then 1 else 0
           end) as january_2018,
       sum(case when match_date >= date '2018-02-01' and 
                     match_date < date '2018-03-01'
                then 1 else 0
           end) as february_2018,
       sum(case when match_date >= date '2018-03-01' and 
                     match_date < date '2018-04-01'
                then 1 else 0
           end) as march_2018,
       sum(case when match_date >= date '2018-01-01' and 
                     match_date < date '2019-01-01'
               then 1 else 0
           end) as total_2018
from match_results
group by grouping sets ( (location, home_team_name), () );

Ou seja, repetir a consulta é desnecessário. Também alterei as comparações de datas para usar datas reais. Acho isso mais legível e sustentável do que extrair partes de data.