Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL GROUP BY - Using COUNT() function

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.

like image 507
Barry McAuley Avatar asked Dec 03 '12 12:12

Barry McAuley


People also ask

Can we use count in GROUP BY in SQL?

The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.

How does count work with GROUP BY?

SQL COUNT( ) with group by and order byThe GROUP BY makes the result set in summary rows by the value of one or more columns. Each same value on the specific column will be treated as an individual group.

How do I count rows in a GROUP BY?

To count the number of rows, use the id column which stores unique values (in our example we use COUNT(id) ). Next, use the GROUP BY clause to group records according to columns (the GROUP BY category above). After using GROUP BY to filter records with aggregate functions like COUNT, use the HAVING clause.

How does count () work in SQL?

The SQL COUNT() function returns the number of rows in a table satisfying the criteria specified in the WHERE clause. It sets the number of rows or non NULL column values. COUNT() returns 0 if there were no matching rows.


1 Answers

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.

like image 149
Taryn Avatar answered Oct 09 '22 14:10

Taryn