3

I am trying to self educate myself in SQL in order to better use databases at work. For this purpose I am using Oracle Application Express. This if my first time using the COUNT function and I am having some difficulties integrating it within my query. I have done a great deal of research and read quite a bit of literature but I just can't seem to get it right.

My goal is to display the channel_name and channel_number columns (from the channel table) for each channel along with a count of the number of customers that have that channel as a favorite channel (survey_result column from the survey table). Please see below for code:

SELECT channel.channel_number, 
       channel.channel_name, 
       survey.survey_result, 
       count(SELECT survey.survey_result FROM survey)
  From Channel, survey
 WHERE survey.channel_number = channel.channel_number

Currently I am getting the error message:

ORA-00936: missing expression.

3 Answers 3

2

Try this:

Below query gives you only those channels which have minimum 1 customer.

SELECT C.channel_number, C.channel_name, COUNT(S.survey_result) NoOfCustomers
FROM Channel C
INNER JOIN survey S ON S.channel_number = C.channel_number
GROUP BY C.channel_number, C.channel_name;

And below query gives you all channels whether it has customer or not.

SELECT C.channel_number, C.channel_name, COUNT(S.survey_result) NoOfCustomers
FROM Channel C
LEFT JOIN survey S ON S.channel_number = C.channel_number
GROUP BY C.channel_number, C.channel_name;
Sign up to request clarification or add additional context in comments.

Comments

2

Either of these may work for you

SELECT channel.channel_number, 
       channel.channel_name, 
       count(survey.survey_result)
  From Channel, survey
 WHERE survey.channel_number = channel.channel_number
 GROUP BY 
       channel.channel_number, 
       channel.channel_name

or

SELECT channel.channel_number, 
       channel.channel_name, 
       survey.survey_result, 
       (SELECT count(survey_result) FROM survey)
  From Channel, survey
 WHERE survey.channel_number = channel.channel_number

1 Comment

first option is the way to go
1

count is an aggregate function thus you should have a group by on channel.channel_number and channel.channel_name. then just use count(survey.survey_result) instead of count(SELECT survey.survey_result FROM survey). Madhivanan's and Saharsh Shah's answers look good to me. including this answer to explain why.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.