query errors when all data not present
count(*)


should become

coalesce(count(*), 1)


for the simplest case. The denominator doesn't actually matter because 0/anything is always zero.



Also:

s.code IN ('10401','10402','10403') OR NULL


is nonsense. count(x OR NULL) is exactly the same as count(x). NULL is false in boolean context, x OR NULL is the same as just saying x. So just write:

s.code IN ('10401','10402','10403')


If what you're actually trying to say is "count rows that are either one of the listed values or are null" then you need a coalesce. You can't just use IN (..., NULL) since x IN (..., NULL) is always either true or NULL, since x IN (...) is equivalent to x = ANY (...) and x = NULL is NULL. So you must write:

s.code IN ('10401','10402','10403') OR s.code IS NULL