Reputation: 21
When I try to execute following block, it returns 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.
Upvotes: 0
Views: 331
Reputation: 21
The error got resolved after using group by on one the columns in my complex query.
For example, in the above given example if you add following group by, it starts working.
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.
Upvotes: 1
Reputation:
The error message points specifically to the 0 in select 0 from dual
; 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). 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". In which case the error message should point to the beginning of the subquery, not to the 0 in it. 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.
Upvotes: 0
Reputation: 1269703
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;
Upvotes: 0