Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8192465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:11:15+00:00 2026-06-07T04:11:15+00:00

I found it hard to find a fitting title. For simplicity let’s say I

  • 0

I found it hard to find a fitting title. For simplicity let’s say I have the following table:

cook_id cook_rating
1       2
1       1
1       3
1       4
1       2
1       2
1       1
1       3
1       5
1       4
2       5
2       2

Now I would like to get an output of ‘good’ cooks. A good cook is someone who has a rating of at least 70% of 1, 2 or 3, but not 4 or 5.

So in my example table, the cook with id 1 has a total of 10 ratings, 7 of which have type 1, 2 and 3. Only three have type 4 or 5. Therefore the cook with id 1 would be a ‘good’ cook, and the output should be the cook’s id with the number of good ratings.

cook_id cook_rating
1       7

The cook with id 2, however, doesn’t satisfy my condition, therefore should not be listed at all.

select cook_id, count(cook_rating) - sum(case when cook_rating = 4 OR cook_rating = 5 then 1 else 0 end) as numberOfGoodRatings from cook
where cook_rating in (1,2,3,4,5)
group by cook_id
order by numberOfGoodRatings desc

However, this doesn’t take into account the fact that there might be more 4 or 5 than good ratings, resulting in negative outputs. Plus, the requirement of at least 70% is not included.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:11:16+00:00Added an answer on June 7, 2026 at 4:11 am

    You can get this with a comparison in your HAVING clause. If you must have just the two columns in the result set, this can be wrapped as a sub-select select cook_id, positive_ratings FROM (...)

    SELECT 
      cook_id, 
      count(cook_rating < 4 OR cook_rating IS NULL) as positive_ratings, 
      count(*) as total_ratings
    FROM cook
    GROUP BY cook_id
    HAVING (positive_ratings / total_ratings) >= 0.70
    ORDER BY positive_ratings DESC
    

    Edit Note that count(cook_rating < 4) is intended to only count rows where the rating is less than 4. The MySQL documentation says that count will only count non-null rows. I haven’t tested this to see if it equates FALSE with NULL but I would be surprised it it doesn’t. Worst case scenario we would need to wrap that in an IF(cook_rating < 4, 1,NULL).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been dealing with ebooks lately, and found it quite hard to quickly find
I found this solution which works, but I find it hard to believe there
I hate IBM's site. It is extremely hard to find anything on there let
I find it very hard to find anything official on this matter. I have
I have completed a red black tree in c and i find it hard
I found the hard way that navigationController.navigationBarHidden = NO; and navigationController.navigationBar.hidden = NO; are
I found it very hard to customize PDF invoice that Magento use. Not only
From hard experience I've found it useful to occasionally save the state of my
Found this is some example CSS I was reading and I'm having a hard
I've found some remnant documents on one of my hard drives that were somehow

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.