Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 889439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:34:04+00:00 2026-05-15T13:34:04+00:00

this is my sql statement i get this error. but when i use only

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:34:04+00:00Added an answer on May 15, 2026 at 1:34 pm

    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:

    SELECT gc1.CategoryName, gc1.SubCategoryName, gc1.BookCount
      FROM (SELECT cat.CategoryName, sb.SubCategoryName,
                   COUNT(bs.BookID) AS BookCount
              FROM Category AS cat
              JOIN SubCategory AS sb ON cat.CategoryID = sb.CategoryID
              JOIN Book_Subcategory AS bs ON sb.SubCategoryID = bs.SubCategoryID
             GROUP BY cat.CategoryName, sb.SubCategoryName
           ) AS gc1
     WHERE gc1.BookCount = (SELECT MAX(gc2.BookCount)
                              FROM (SELECT cat.CategoryName, sb.SubCategoryName,
                                           COUNT(bs.BookID) AS BookCount
                                      FROM Category AS cat
                                      JOIN SubCategory AS sb
                                           ON cat.CategoryID = sb.CategoryID
                                      JOIN Book_Subcategory AS bs
                                           ON sb.SubCategoryID = bs.SubCategoryID
                                     GROUP BY cat.CategoryName, sb.SubCategoryName
                                    ) AS gc2
                            )
    

    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):

    WITH gc1 AS (SELECT cat.CategoryName, sb.SubCategoryName,
                        COUNT(bs.BookID) AS BookCount
                   FROM Category AS cat
                   JOIN SubCategory AS sb
                        ON cat.CategoryID = sb.CategoryID
                   JOIN Book_Subcategory AS bs
                        ON sb.SubCategoryID = bs.SubCategoryID
                  GROUP BY cat.CategoryName, sb.SubCategoryName
                )
    SELECT gc1.CategoryName, gc1.SubCategoryName, gc1.BookCount
      FROM gc1
     WHERE gc1.BookCount = SELECT MAX(gc1.BookCount) FROM gc1);
    

    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:

    SELECT cat.CategoryName, sb.SubCategoryName,
           COUNT(bs.BookID) AS BookCount
      FROM Category AS cat
      JOIN SubCategory AS sb ON cat.CategoryID = sb.CategoryID
      JOIN Book_Subcategory AS bs ON sb.SubCategoryID = bs.SubCategoryID
     GROUP BY cat.CategoryName, sb.SubCategoryName
      INTO TEMP gc1;
    
    SELECT gc1.CategoryName, gc1.SubCategoryName, gc1.BookCount
      FROM gc1
     WHERE gc1.BookCount = (SELECT MAX(gc1.BookCount) FROM gc1);
    
    DROP TABLE gc1;  -- Optional: table will be deleted at end of session anyway
    

    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:

    Non-Fiction    SQL Theory    4
    Fiction        War           4
    

    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

    CREATE TABLE Category
    (
        CategoryID INTEGER NOT NULL PRIMARY KEY,
        CategoryName VARCHAR(20) NOT NULL
    );
    CREATE TABLE SubCategory
    (
        CategoryID INTEGER NOT NULL REFERENCES Category,
        SubCategoryID INTEGER NOT NULL PRIMARY KEY,
        SubCategoryName VARCHAR(20) NOT NULL
     );
    CREATE TABLE Book_SubCategory
    (
        SubCategoryID INTEGER NOT NULL REFERENCES SubCategory,
        BookID INTEGER NOT NULL PRIMARY KEY
    );
    

    Data

    INSERT INTO Category VALUES(1, 'Fiction');
    INSERT INTO Category VALUES(2, 'Non-Fiction');
    
    INSERT INTO SubCategory VALUES(2, 1, 'SQL Theory');
    INSERT INTO SubCategory VALUES(2, 2, 'Mathematics');
    INSERT INTO SubCategory VALUES(1, 3, 'Romance');
    INSERT INTO SubCategory VALUES(1, 4, 'War');
    
    INSERT INTO Book_SubCategory VALUES(1, 10);
    INSERT INTO Book_SubCategory VALUES(2, 11);
    INSERT INTO Book_SubCategory VALUES(3, 12);
    INSERT INTO Book_SubCategory VALUES(3, 13);
    INSERT INTO Book_SubCategory VALUES(4, 14);
    INSERT INTO Book_SubCategory VALUES(1, 15);
    INSERT INTO Book_SubCategory VALUES(1, 16);
    INSERT INTO Book_SubCategory VALUES(2, 17);
    INSERT INTO Book_SubCategory VALUES(1, 18);
    INSERT INTO Book_SubCategory VALUES(3, 19);
    INSERT INTO Book_SubCategory VALUES(4, 20);
    INSERT INTO Book_SubCategory VALUES(4, 21);
    INSERT INTO Book_SubCategory VALUES(4, 22);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 429k
  • Answers 429k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Apparently this is not a new problem. Here's a sampling… May 15, 2026 at 1:34 pm
  • Editorial Team
    Editorial Team added an answer For reference, you could also do it without hierarchical extensions… May 15, 2026 at 1:34 pm
  • Editorial Team
    Editorial Team added an answer The dot added to the name is used by the… May 15, 2026 at 1:34 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.