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?
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.
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.
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.
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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With