Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not a single-group group function

Tags:

sql

oracle

I have some tables which basically look as follows:

TBL_USER
user_id - number
user_name - varchar

TBL_STUFF
stuff_id - number
stuff_user_id - number

I want to query for all user information including the number of "stuff" they have. I was trying something like this:

select user_id, user_name, count(stuff_id) 
  from tbl_user
  left outer join tbl_stuff on stuff_user_id = user_id
 where user_id = 5;

but I get an error which says "not a single-group group function"

Is there some other way I should be doing this?

like image 509
Rocky Pulley Avatar asked Apr 18 '12 13:04

Rocky Pulley


People also ask

Which function is not a group function?

Which of the following is NOT a GROUP BY function? Answer: C. NVL is a general function used to provide alternate value to the NULL values. The functions MAX, MIN and AVG can be used as GROUP BY functions.

How do I fix Ora 00937 Not a single group group function?

The Solution To resolve the error, you can either remove the group function or column expression from the SELECT clause or you can add a GROUP BY clause that includes the column expressions.

What group function is not allowed?

ORA-00934: group function is not allowed here Cause: One of the group functions, such as AVG, COUNT, MAX, MIN, SUM, STDDEV, or VARIANCE, was used in a WHERE or GROUP BY clause. Action: Remove the group function from the WHERE or GROUP BY clause.

Is 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.


2 Answers

Well, you are missing the group function ;-)

Try this:

select user_id, user_name, count(stuff_id) 
from tbl_user left outer join tbl_stuff on stuff_user_id = user_id
where user_id = 5
group by user_id, user_name;

The last line is the group by clause that tells Oracle to count all rows with the same user_id and user_name combination.

like image 95
Daniel Hilgarth Avatar answered Sep 19 '22 15:09

Daniel Hilgarth


You could also do it like this:

select 
  user_id, 
  user_name, 
  (
    SELECT
        COUNT(*)
    FROM
        tbl_stuff
    WHERE 
        stuff_user_id = tbl_user.user_id

  ) AS StuffCount, 
from 
   tbl_user
where 
   user_id = 5;
like image 30
Arion Avatar answered Sep 18 '22 15:09

Arion