sql - ORA-00937: 不是单组组函数

Posted

技术标签:

【中文标题】sql - ORA-00937: 不是单组组函数【英文标题】:sql - ORA-00937: not a single-group group function 【发布时间】:2019-01-12 12:04:15 【问题描述】:
 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;

错误消息:ORA-00937:不是单组组函数。目前在 oracle live SQL 上运行。

关于如何解决这个问题的任何想法?

【问题讨论】:

group by 移动到第一个选择。并查看grouping sets 以在一次选择中获得两者。 【参考方案1】:

我推荐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), () );

也就是说,不需要重复查询。我还更改了日期比较以使用实际日期。我发现这比提取日期部分更具可读性和可维护性。

【讨论】:

@sammywemmy 。 . .我发现您更喜欢更详细的解决方案,这让我感到惊讶。 我从这两个答案中学到了很多,对sql的理解也更多了。不是一个比一个,我只能勾选一个答案【参考方案2】:

运行聚合查询时,每个非聚合列都必须出现在GROUP BY 子句中。您有两个 UNIONed 子查询:并且只有第一个具有非聚合列(即 locationhome_team),但是您碰巧将 GROUP BY 子句放在第二个子查询中,因为它似乎计算总数,可能不需要一个。您可以将GROUP BY 子句放在第一个查询中而不是第二个:

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
;

【讨论】:

谢谢。我真的很感激

以上是关于sql - ORA-00937: 不是单组组函数的主要内容,如果未能解决你的问题,请参考以下文章

ORA-00937: 不是单组组函数错误

ORA-00937: 不是 oracle 中的单组组函数

Oracle的“ORA-00937: 不是单组分组函数” 如何解决?

ORA-00937: "不是单组群函数"

ORA-00937: 不是单组分组函数,已在使用 group by

关于 ORA-00937不是单组分组函数的解决办法