ORA-00937: 不是单组分组函数,已在使用 group by
Posted
技术标签:
【中文标题】ORA-00937: 不是单组分组函数,已在使用 group by【英文标题】:ORA-00937: not a single-group group function, already using group by 【发布时间】:2021-11-07 08:37:26 【问题描述】:知道为什么这个查询会发生这个错误吗?
00000 - “不是单组群功能” *原因: *Action: Erro na linha: 1 Coluna: 8
select po.plop_sq_planej_oper,
port.rtsi_cd_rtsubitem,
(count(*)-1) as qnt_lista
from sigiop.planejamento_operacional po,
sigiop.planejamento_operacional_rt port,
sigiop.rt_subitem rtsi,
(select po1.plop_sq_planej_oper,
po1.plop_dt_criacao,
rtsi1.rtit_cd_rtitem
from sigiop.planejamento_operacional po1,
sigiop.planejamento_operacional_rt port1,
sigiop.rt_subitem rtsi1
where po1.plop_sq_planej_oper = port1.plop_sq_planej_oper
and port1.rtsi_cd_rtsubitem = rtsi1.rtsi_cd_rtsubitem
group by po1.plop_sq_planej_oper,
po1.plop_dt_criacao,
rtsi1.rtit_cd_rtitem) port1
where po.plop_sq_planej_oper = port.plop_sq_planej_oper
and port.rtsi_cd_rtsubitem = rtsi.rtsi_cd_rtsubitem
and po.plop_dt_criacao >= port1.plop_dt_criacao(+)
and rtsi.rtit_cd_rtitem = port1.rtit_cd_rtitem(+)
【问题讨论】:
您的GROUP BY
位于内部SELECT
(FROM
语句中的那个),但您的COUNT
位于外部SELECT
中,这就是错误所指的出去。你的内部SELECT
也没有使用任何聚合函数,所以GROUP BY
不适用于它。
您使用的是count(*)
,但主查询中没有GROUP BY
,
另外,请使用当前的连接语法而不是连接形式 1989。
【参考方案1】:
问题是查询以
开头select po.plop_sq_planej_oper,
port.rtsi_cd_rtsubitem,
(count(*)-1) as qnt_lista
但没有结束
group by po.plop_sq_planej_oper, port.rtsi_cd_rtsubitem
我还建议使用标准 ANSI 连接来编写它,因为
select po.plop_sq_planej_oper
, port.rtsi_cd_rtsubitem
, count(*) - 1 as qnt_lista
from sigiop.planejamento_operacional po
join sigiop.planejamento_operacional_rt port
on port.plop_sq_planej_oper = po.plop_sq_planej_oper
join sigiop.rt_subitem rtsi
on rtsi.rtsi_cd_rtsubitem = port.rtsi_cd_rtsubitem
left join
( select distinct
po1.plop_sq_planej_oper
, po1.plop_dt_criacao
, rtsi1.rtit_cd_rtitem
from sigiop.planejamento_operacional po1
join sigiop.planejamento_operacional_rt port1
on port1.plop_sq_planej_oper = po1.plop_sq_planej_oper
join sigiop.rt_subitem rtsi1
on rtsi1.rtsi_cd_rtsubitem = port1.rtsi_cd_rtsubitem
) port1
on port1.plop_dt_criacao <= po.plop_dt_criacao
and port1.rtit_cd_rtitem = rtsi.rtit_cd_rtitem
group by
po.plop_sq_planej_oper
, port.rtsi_cd_rtsubitem;
【讨论】:
【参考方案2】:您需要按以下方式修复您的查询 -
SELECT po.plop_sq_planej_oper,
port.rtsi_cd_rtsubitem,
(count(*)-1) as qnt_lista
FROM sigiop.planejamento_operacional po
JOIN sigiop.planejamento_operacional_rt port ON po.plop_sq_planej_oper = port.plop_sq_planej_oper
JOIN sigiop.rt_subitem rtsi ON port.rtsi_cd_rtsubitem = rtsi.rtsi_cd_rtsubitem
LEFT JOIN (SELECT po1.plop_sq_planej_oper,
po1.plop_dt_criacao,
rtsi1.rtit_cd_rtitem
FROM sigiop.planejamento_operacional po1
JOIN sigiop.planejamento_operacional_rt port1 ON po1.plop_sq_planej_oper = port1.plop_sq_planej_oper
JOIN sigiop.rt_subitem rtsi1 ON port1.rtsi_cd_rtsubitem = rtsi1.rtsi_cd_rtsubitem
GROUP BY po1.plop_sq_planej_oper,
po1.plop_dt_criacao,
rtsi1.rtit_cd_rtitem) port1 ON po.plop_dt_criacao >= port1.plop_dt_criacao
AND rtsi.rtit_cd_rtitem = port1.rtit_cd_rtitem
GROUP BY po.plop_sq_planej_oper,
port.rtsi_cd_rtsubitem;
【讨论】:
外层SELECT列表包括聚合项和非聚合项还有问题,非聚合项不包含在GROUP BY子句中。 @WilliamRobertson,我没有注意到这一点。感谢您的关注。以上是关于ORA-00937: 不是单组分组函数,已在使用 group by的主要内容,如果未能解决你的问题,请参考以下文章