简体   繁体   English

简单 Case 语句返回 ORA-00937: not a single-group group function

[英]Simple Case statement returns ORA-00937: not a single-group group function

When I try to execute following block, it returns ORA-00937.当我尝试执行以下块时,它返回 ORA-00937。 Can someone help what is happening here?有人可以帮助这里发生的事情吗?

select case when count(1)=0 then (select 0 from dual)
          else (select 1 from dual )
          end  test1 
from dual;

This is just simplified example I tried and I am facing this error.这只是我尝试过的简化示例,我正面临这个错误。 I have a rather complex query which has similar structure but failing with same error.我有一个相当复杂的查询,它具有相似的结构,但由于相同的错误而失败。

Aggregation queries are not friendly to subqueries.聚合查询对子查询不友好。 You probably already know what you can write:你可能已经知道你可以写什么:

select (case when count(*) = 0 then 0 else 1 end) as test1
from dual;

Or more concisely:或更简洁地说:

select sign(count(*)) as test1
from dual;

The error message points specifically to the 0 in select 0 from dual ;错误消息专门指向select 0 from dual中的 0 ; why it points to that specifically is not clear, since 0 itself is a constant (and therefore it is always a GROUP BY expression, no matter what else is happening).为什么它特别指出这一点尚不清楚,因为 0 本身是一个常量(因此它始终是一个 GROUP BY 表达式,无论发生什么其他事情)。 It is more likely that Gordon Linoff is right: the scalar subquery itself is the issue, since Oracle doesn't evaluate it "now" (at compilation time), it just says "I can't assume it will turn out to be a constant". Gordon Linoff 更有可能是对的:标量子查询本身就是问题,因为 Oracle 没有“现在”(在编译时)评估它,它只是说“我不能假设它会变成持续的”。 In which case the error message should point to the beginning of the subquery, not to the 0 in it.在这种情况下,错误消息应该指向子查询的开头,而不是其中的 0。 Oh well...那好吧...

Anyway: if you want to rewrite it in an equivalent way, that will be valid from the outset, you could do something like this:无论如何:如果您想以等效的方式重写它,从一开始就有效,您可以执行以下操作:

select case when exists (select * from dual) then [the first thing]
       else [the other thing] end ....

Replace your count(1) = 0 with an exists condition - the difference is that there's no aggregation involved.exists条件替换您的count(1) = 0 - 不同之处在于不涉及聚合。

The error got resolved after using group by on one the columns in my complex query.在我的复杂查询中的一个列上使用 group by 后,该错误得到解决。

For example, in the above given example if you add following group by, it starts working.例如,在上面给出的示例中,如果您添加以下 group by,它就会开始工作。

select case when count(1)=0 then (select 0 from dual)
          else (select 1 from dual )
          end  test1 
from dual group by dummy;

Note : The column on which we are applying group by, must have same value for all the rows.注意:我们应用分组依据的列必须对所有行具有相同的值。

Sorry for being late to respond on this.很抱歉迟到对此作出回应。 Thanks everyone for the comments.多谢大家的评价。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM