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?
When running an aggregate query, every non-aggregated column must appear in the
GROUP BY
clause. You have twoUNION
ed subquery : and only the first one has non-aggregated columns (namely,location
andhome_team
), however you happened to put theGROUP BY
clause in the second one, that, as it seems to compute totals, probably does not need one. You could just place theGROUP BY
clause in the first query instead of the second :I recommend
grouping sets
:That is, repeating the query is unnecessary. I also changed the date comparisons to use actual dates. I find this more readable and maintainable than extracting date parts.