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?
The error says it all, you’re not grouping by
MEMBERS.MEMBER_IDandMEMBERS.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:
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
MEMBERStable is unique onMEMBER_ID, but based on your query I suspect it is. To clarify what I mean, if your table is not unique onMEMBER_IDthen you’re not counting the number of sessions perMEMBER_IDbut the number of sessions perMEMBER_IDand perMEMBER_NAME. If they’re in a 1:1 relationship then it’s effectively the same thing but if you can have multipleMEMBER_NAMEs perMEMBER_IDthen it’s not.