【问题标题】:Error 00937 "not a single-group group function"错误 00937“不是单组组函数”
【发布时间】:2021-06-15 11:39:14
【问题描述】:

我遇到了 ORA-00937: "not a single-group group function" 这个 sql 查询的错误问题: 请有任何想法!谢谢。

SELECT avg(count(*)) as value, 'Taux remplissage' as serie, to_char(c.datcre, 'YYYY-MM-DD') as arg
from charge c left join emplac e ON c.adr = e.adr 
where e.ADR is not null and e.empsta != 'I' and e.empsta != 'V'
and (trunc(c.datcre) >= to_date('2020-11-01','YYYY-MM-DD'))
and (trunc(c.datcre) <= to_date('2021-11-30','YYYY-MM-DD'))
GROUP BY to_char(c.datcre, 'YYYY-MM-DD')
ORDER BY arg, serie

【问题讨论】:

  • 问题是avg(count(*))。你能解释一下你想要完成什么吗?
  • @GordonLinoff: 我想计算一个商店的填充率(平均值)
  • 。 .我应该更清楚:在问题中提供样本数据、期望、结果和清晰的解释。

标签: sql oracle


【解决方案1】:

在这种情况下,我不确定您所说的 avg(count(*)) 是什么意思。这将用于(在 Oracle 中)您想要一个包含一行的结果集的上下文中。但是order by 表明您需要多行。

如果您只想要count(*),那么您可以使用:

select count(*) as value, 'Taux remplissage' as serie, 
       to_char(c.datcre, 'YYYY-MM-DD') as arg
from charge c join
     emplac e 
     ON c.adr = e.adr 
where e.ADR is not null and e.empsta not in ('I', 'V') and
      c.datcre >= date '2020-11-01' and
      c.datcre < date '2021-12-01'
group by to_char(c.datcre, 'YYYY-MM-DD')
order by arg;

如果您想要每一行的总体平均值,您可以使用:

avg(count(*)) over () as average

注意查询的其他更改:

  • not in&lt;&gt;s 的链要简单得多。
  • 严格来说,is not null 是多余的,但我把它留在了里面。
  • where 子句将left join 转换为inner join,因此您应该指定您实际使用的join
  • Oracle 支持日期常量的标准 SQL 语法。您不妨使用它。
  • 当您删除列上的函数时,日期比较通常会更有效。这有助于优化器。

【讨论】:

    【解决方案2】:
    SELECT count(*) * 100.0 / (select count(*) from emplac) as value,'% remplissage ' || c.mag as serie, to_char(c.datcre, 'YYYY-MM-DD') as arg
    from charge c left join emplac e ON c.adr = e.adr 
    where e.ADR is not null and e.empsta != 'I'
    and (trunc(c.datcre) >= to_date('2020-11-01','YYYY-MM-DD'))
    and (trunc(c.datcre) <= to_date('2021-11-30','YYYY-MM-DD'))
    GROUP BY to_char(c.datcre, 'YYYY-MM-DD'), c.mag
    ORDER BY arg
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      相关资源
      最近更新 更多