Solved

ora-00937 not single-group group function

Posted on 2004-09-20
5
632 Views
Last Modified: 2013-12-11
for some reason a SELECT of a DECODE mixing the built-in aggregate function COUNT(*) with single-valued fields
causes ora-00937. Adding a GROUP BY clause, as suggested by Henka, only causes a different error to be produced
- ora-00979.

Now slightwv claims it always works for constants and literals (i.e. non-tabular values), which is precisely why it is
useless, even if it does work. His/her/its second suggestion to split the query into 2, by first assigning COUNT(*) INTO a
local variable and then using this var in the desired query is the one I ran away from.

Unfortunately, this site does not allow to continue discussing a problem in a thread, so each time I get an unsatisfactory
reply I have to post a new question as though there never were any previous questions and answers. There's a hyperlink
"Feedback" above the reply but it leads to an editor to provide a Feedback to the question, not to the reply. That's
extremely confusing. It may be that my "Feedback" would land under the same thread, but why can't I see the whole
goddamn thread while I'm feeding back my response is beyond me.

Anyways, my initial Q is here:  http://www.experts-exchange.com/Databases/Oracle/Product_Info/Q_21136044.html
As you can see, the problem arose when I tried to get rid of BASIC programming with IF, ELSE and END, and
express the function as a pure SQL-Query. A purely SQL-wise expression in a function has the advantage that it can
relatively easily be transplanted into a VIEW to get rid of costly function invocation. Once a VIEW is independent of any
functions, CBO can work out severely improved QEPs. I've seen a 30-fold speed-up in such cases. By splitting the query into two queries, you lose this capability. Next answer, please.

Just in case someone else would like to see my second question and slightwv's reply, it is here:
 http://www.experts-exchange.com/Databases/Oracle/Product_Info/Q_21137692.html

I'll obviously have to up the ante now in the hope an SQL-guru might bite. The query is typical replacement for a number
of functions in which a SELECT COUNT(*) INTO LocalVar FROM aTable WHERE someCondition; precedes an
IF LocalVar > 0 THEN doThis END construct. In most cases, this precaution is totally superfluous, because the query yields
NULL value anyway if someCondition is not satisfied. However, testing explicitly for the presence of a row satisfying
someCondition, tends to be perceived by lay managementry as somehow better guaranteeing conformance to the old
functional school. To put it bluntly, it has to be there so that I don't have to low-brow my peers and bosses.

Now, I would also grant all the points to someone who can explain why it's impossible, if that's the case.
0
Comment
Question by:dkrnic
  • 2
5 Comments
 
LVL 22

Expert Comment

by:Helena Marková
ID: 12109376
I think that there was no need of opening a new questions. You can put your comments to the original question ...
0
 
LVL 1

Accepted Solution

by:
oliversdaddy earned 500 total points
ID: 12180438
Hi,
You can do this with an 'inline view'
Try:

        SELECT  decode(acount, 0,NULL, decode(ptest,0,NULL,'P'))
        INTO    sAName
from
(
        select count(*) acount, ptest
        FROM    A
        WHERE   A_ID = iA_ID;
        group by ptest
)

Regards, Steve
0
 

Author Comment

by:dkrnic
ID: 12181064
That's an interesting twist, but the problem has been resolved
in a different thread. Basically the above example is an oversimplification
of the actual query, which involves some dozens of decodes and the
way to make it work with count(*) without causing the error is to
include all of the fields in the GROUP BY clause.
0
 

Author Comment

by:dkrnic
ID: 12399216
Thanks, Steve.

your solution is elegant.

Since I already came to terms with the bug,
I've fogot that a thread has to be closed some time.
But not without credit, where credit is due.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dataware house query tuning 9 64
Concat multi row values of a field in oracle 6 56
Fastest way to replace data in Oracle 5 51
sort a spool into file output in oracle 1 22
Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now