In my websites authors can upload their books. I display statistics on each page and one of the stats I display is the average rating of the author's books. I take the sum of all the ratings, and divide it by the number of books:
select
sum(BookRate)/count(BookID) as AvrageRate
from Books
where Author = "Some Author"
The problems appear when the user has not yet uploaded any books and an obvious error message is returned:
Divide by zero error encountered.
Is there a way to avoid this situation within the SQL query or do I have to first check the book count with one query, and then run another query for the stats?
Thanks world.
whereclause returns no rows theSUM(BookRate)will beNULLand that trumps the divide by zero.