Answer the question
In order to leave comments, you need to log in
How to return a default value in a query if SELECT returns NULL or returns nothing for a particular expression?
Good afternoon!
For example, I want the bottom expression to return 0 to me or, say , 1 if the result of the rating expression returns NULL :
SELECT
m_users.id,
( SUM(mc_rating.name) / COUNT(mc_rating.name)) as rating ...
Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question