I have a table named ReviewFact which has the following attributes:
- CategoryID (Primary Key)
- Star_Rating (Primary Key)
- No_of_Reviews
CategoryID refers to specific product, Star_Rating refers to a star rating given by users, No_of_Reviews refers to how many users gave this rating.
I have to write an SQL query which shows the number of 5 star reviews for each category.
I have come up with the following:
SELECT r.CategoryID, r.NUMBER_OF_REVIEWS
FROM REVIEWFACT r
WHERE r.Stars = 5
ORDER BY r.NUMBER_OF_REVIEWS desc;
Using this I get the number of reviews for rows which have 5 star reviews, but not for ones which don't. The ones which don't should have the count as 0. How do I go about solving this?
If there are the following rows in the table:
CategoryID Star_Rating No_of_Reviews
1 5 10
2 5 4
3 2 9
The query should return
CategoryID No_of_Reviews
1 10
2 4
3 0