SELECT
m_users.id,
( SUM(mc_rating.name) / COUNT(mc_rating.name)) as rating ...
select coalesce( ( SUM(mc_rating.name) / COUNT(mc_rating.name) ), 0::integer ) as rating, ...
with _rating as ( select
m_users.id,
coalesce( ( SUM(mc_rating.name) / COUNT(mc_rating.name) ), 0::integer ) as rating ....
)
select * from _rating r where r.rating > 0