Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not a GROUP BY expression error [duplicate]

I'm relatively new to databases. I am using Oracle and I'm trying to implement this query to find the number of personal training sessions the member has had.

The tables are;

MEMBERS

MEMBERS_ID(NUMBER),
MEMBERSHIP_TYPE_CODE(VARCHAR),
ADDRESS_ID(NUMBER), CLUB_ID(NUMBER) 
MEMBER_NAME(VARCHAR), 
MEMBER_PHONE(VARCHAR), 
MEMBER_EMAIL(VARCHAR)

PERSONAL_TRAINING_SESSIONS

SESSION_ID(VARHCAR), 
MEMBER_ID (NUMBER), 
STAFF_ID(VARCHAR), 
SESSION_DATETIME(DATE)

My query is returing this error:

ORA-00979: not a GROUP BY expression 00979. 00000 - "not a GROUP BY expression" *Cause:
*Action: Error at Line: 1 Column: 8

SELECT MEMBERS.MEMBER_ID,MEMBERS.MEMBER_NAME, COUNT(personal_training_sessions.session_id)
FROM MEMBERS JOIN personal_training_sessions
ON personal_training_sessions.member_id=members.member_id
GROUP BY personal_training_sessions.session_id;

Can anyone point me in the right direction? I have looked around do I need to separate the count query?

like image 497
memyselfandmyiphone Avatar asked Dec 19 '12 23:12

memyselfandmyiphone


People also ask

How do you fix not GROUP BY expression?

To resolve the ORA-00979: not a group by expression error, simply ensure that all of the GROUP BY columns match the SELECT clause. You can do this by adding columns to the GROUP BY. The columns don't need to be in the same order to correct the error.

What does it mean not a GROUP BY expression?

ORA-00979 “ Not a GROUP BY expression ” is an error issued by the Oracle database when the SELECT statement contains a column that is neither listed in GROUP BY nor aggregated. This error message can be confusing to beginners. Practice your SQL knowledge with our SQL Practice Set course.

Is not a valid GROUP BY expression?

This SQL error means that that database is trying to group on something that it can't. Usually, this means that there are aggregates in a dimension definition.

What is Ora-00979 Not A GROUP BY expression?

ORA-00979 occurs when the GROUP BY clause does not contain all the expressions in the SELECT clause. Any SELECT expression that is not included in the GROUP function must be listed in the GROUP BY clause. These are AVG, COUNT, MAX, MIN, SUM, STDDEV, and VARIANCE.


1 Answers

The error says it all, you're not grouping by MEMBERS.MEMBER_ID and MEMBERS.MEMBER_NAME.

SELECT MEMBERS.MEMBER_ID, MEMBERS.MEMBER_NAME
     , COUNT(personal_training_sessions.session_id)
  FROM MEMBERS 
  JOIN personal_training_sessions
    ON personal_training_sessions.member_id = members.member_id
 GROUP BY MEMBERS.MEMBER_ID, MEMBERS.MEMBER_NAME

You want the count of personal sessions per member, so you need to group by the member information.

The basic (of course it can get a lot more complex) GROUP BY, SELECT query is:

SELECT <column 1>, <column n>
     , <aggregate function 1>, <aggregate function n>
  FROM <table_name>
 GROUP BY <column 1>, <column n>

An aggregate function being, as Ken White says, something like MIN(), MAX(), COUNT() etc. You GROUP BY all the columns that are not aggregated.

This will only work as intended if your MEMBERS table is unique on MEMBER_ID, but based on your query I suspect it is. To clarify what I mean, if your table is not unique on MEMBER_ID then you're not counting the number of sessions per MEMBER_ID but the number of sessions per MEMBER_ID and per MEMBER_NAME. If they're in a 1:1 relationship then it's effectively the same thing but if you can have multiple MEMBER_NAMEs per MEMBER_ID then it's not.

like image 67
Ben Avatar answered Sep 27 '22 20:09

Ben