aggregate of an empty result set

后端 未结 4 1667
一向
一向 2021-02-13 19:17

I would like the aggregates of an empty result set to be 0. I have tried the following:

SELECT SUM(COALESCE(capacity, 0))
  FROM objects
 WHERE null IS NOT NULL;         


        
4条回答
  •  情书的邮戳
    2021-02-13 19:39

    The thing is, the aggregate always returns a row, even if no rows were aggregated (as is the case in your query). You summed an expression over no rows. Hence the null value you're getting.

    Try this instead:

    select coalesce(sum(capacity),0)
    from objects
    where false;
    

提交回复
热议问题