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

  • SEARCH
  • Home
  • 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 8920907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:23:52+00:00 2026-06-15T06:23:52+00:00

I’ve been doing a task which involves creating a database for a hospital and

  • 0

I’ve been doing a task which involves creating a database for a hospital and I’ve been encountering a very frustrating error which I cannot seem to fix no matter how much research that I do.

The error I received is:

ERROR at line 1: ORA-00979: not a GROUP BY expression

The structure of my code for inserting the values is:

SELECT  CONSULTANT.S_NO, DOCTOR.D_NAME, CONSULTANT.SPEC, 
PATIENT.P_ID, PATIENT.P_NAME, COUNT(CONSULTANT.P_ID)
FROM    PATIENT, CONSULTANT, DOCTOR                            
WHERE   PATIENT.P_ID = CONSULTANT.P_ID
AND     CONSULTANT.S_NO = DOCTOR.S_NO
GROUP BY CONSULTANT.S_NO;

And the structure of my tables are:

    CREATE TABLE PATIENT (
    P_ID        NUMBER NOT NULL,
    P_NAME      CHAR(20),
    ADDRESS     VARCHAR(20),
    DOB     DATE,
    WARD_NO     NUMBER NOT NULL, 
    C_S_NO      NUMBER NOT NULL,
    CONSTRAINT PK_PATIENT PRIMARY KEY(P_ID)
);

CREATE TABLE DOCTOR (
    S_NO        NUMBER NOT NULL,
    D_NAME      CHAR(20),
    APP_DATE    DATE,
    CONSTRAINT PK_DOC PRIMARY KEY(S_NO)
);

CREATE TABLE CONSULTANT (
    S_NO        NUMBER NOT NULL,
    P_ID        NUMBER NOT NULL,
    SPEC        CHAR(20),
    T_CODE      VARCHAR(20) NOT NULL,
    CONSTRAINT PK_CDOC PRIMARY KEY(S_NO)
);

Would really appreciate any help anyone could give me on solving this dilemma.

  • 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-06-15T06:23:53+00:00Added an answer on June 15, 2026 at 6:23 am

    Since you are using an aggregate function, your fields in the SELECT list that are not being aggregated need to be in the GROUP BY:

    SELECT  CONSULTANT.S_NO, DOCTOR.D_NAME, CONSULTANT.SPEC, 
    PATIENT.P_ID, PATIENT.P_NAME, COUNT(CONSULTANT.P_ID)
    FROM    PATIENT, CONSULTANT, DOCTOR                            
    WHERE   PATIENT.P_ID = CONSULTANT.P_ID
       AND     CONSULTANT.S_NO = DOCTOR.S_NO
    GROUP BY CONSULTANT.S_NO, DOCTOR.D_NAME, CONSULTANT.SPEC, PATIENT.P_ID, PATIENT.P_NAME
    

    As a side note, I would also use ANSI JOIN syntax instead of the comma separated list of tables:

    SELECT  c.S_NO, d.D_NAME, c.SPEC, p.P_ID, p.P_NAME, COUNT(c.P_ID)
    FROM    PATIENT p
    INNER JOIN CONSULTANT c
      ON p.P_ID = c.P_ID
    INNER JOIN DOCTOR d
      ON c.S_NO = d.S_NO
    GROUP BY c.S_NO, d.D_NAME, c.SPEC, p.P_ID, p.P_NAME
    

    Now, since you need to add the additional fields to the GROUP BY this could adjust the COUNT() total to numbers that you are not expecting. So you might need to incorporate a sub-query to get the total count, similar to this:

    SELECT  c1.S_NO, d.D_NAME, c1.SPEC, p.P_ID, p.P_NAME, c2.Count_P_ID
    FROM    PATIENT p
    INNER JOIN CONSULTANT c1
      ON p.P_ID = c1.P_ID
    INNER JOIN
    (
      select COUNT(c.P_ID) Count_P_ID, S_NO
      from CONSULTANT c
      group by S_NO
    ) c2
      ON c1.S_NO = c2.S_NO
    INNER JOIN DOCTOR d
      ON c1.S_NO = d.S_NO
    

    This allows you to then GROUP BY the one field that you initially wanted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Seemingly simple, but I cannot find anything relevant on the web. What is the
I would like to run a str_replace or preg_replace which looks for certain words
I have an autohotkey script which looks up a word in a bilingual dictionary
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.