this is my sql statement i get this error. but when i use only Max to single and without displaying other results it works. can someone help me
SELECT cat.CategoryName,sb.SubCategoryName,MAX((COUNT(bs.BookID)))
FROM
Category cat,SubCategory sb, Book_Subcategory bs
WHERE cat.CategoryID = sb.CategoryID AND sb.SubCategoryID = bs.SubCategoryID
GROUP BY cat.CategoryName, sb.SubCategoryName, bs.BookID;
ERROR at line 1:
ORA-00937: not a single-group group function
Can someone help me?
SQL does not allow aggregates of aggregates directly.
However, if you write the inner aggregate in a sub-query in the FROM clause (or use a WITH clause and a Common Table Expression, CTE), you can achieve the result:
This is complex because it doesn’t use a CTE, and there is a common table expression that must be written out twice.
Using the CTE form (possibly with syntax errors):
Much tidier!
You can simulate a CTE with a temporary table if your DBMS makes it easy to create them. For example, IBM Informix Dynamic Server could use:
Given the following tables and data, the main query (copied and pasted from this answer) gave the result I expected when run against IBM Informix Dynamic Server 11.50.FC6 on MacOS X 10.6.4, namely:
That doesn’t prove that it ‘must work’ when run against Oracle – I don’t have Oracle and can’t demonstrate either way. It does show that there is at least one SQL DBMS that handles the query without problems. (Since IDS does not support the WITH clause and CTEs, I can’t show whether that formulation works.)
Schema
Data