Search code examples
sqloracle-databaseora-00937

sql - ORA-00937: not a single-group group function


 select
    location,
    home_team_name,
    count(case when extract(year from match_date)='2018' and extract(month from match_date)=1 then 1 end) january_2018,
    count(case when extract(year from match_date)='2018' and extract(month from match_date)=2 then 1 end) february_2018,
    count(case when extract(year from match_date)='2018' and extract(month from match_date)=3 then 1 end) march_2018,
    count(case when extract(year from match_date)='2018' then 1 end) Total
from match_results

union all

select 
    'total' as location,
    'total' as home_team_name,
    count(case when extract(month from match_date)=1 then 1 end) january_2018,
    count(case when extract(month from match_date)=2 then 1 end) february_2018,
    count(case when extract(month from match_date)=3 then 1 end) march_2018,
    count(case when extract(year from match_date)='2018' then 1 end) Total
from match_results
group by location,home_team_name;

Error message: ORA-00937: not a single-group group function. Currently running this on oracle live SQL.

Any ideas on how I can solve this?


Solution

  • When running an aggregate query, every non-aggregated column must appear in the GROUP BY clause. You have two UNIONed subquery : and only the first one has non-aggregated columns (namely, location and home_team), however you happened to put the GROUP BY clause in the second one, that, as it seems to compute totals, probably does not need one. You could just place the GROUP BY clause in the first query instead of the second :

    select
        location,
        home_team_name,
        count(case when extract(year from match_date)='2018' and extract(month from match_date)=1 then 1 end) january_2018,
        count(case when extract(year from match_date)='2018' and extract(month from match_date)=2 then 1 end) february_2018,
        count(case when extract(year from match_date)='2018' and extract(month from match_date)=3 then 1 end) march_2018,
        count(case when extract(year from match_date)='2018' then 1 end) Total
    from match_results
    group by location,home_team_name
    
    union all
    
    select 
        'total' as location,
        'total' as home_team_name,
        count(case when extract(month from match_date)=1 then 1 end) january_2018,
        count(case when extract(month from match_date)=2 then 1 end) february_2018,
        count(case when extract(month from match_date)=3 then 1 end) march_2018,
        count(case when extract(year from match_date)='2018' then 1 end) Total
    from match_results
    ;