The following bug has been logged on the website:
Bug reference: 15208
Logged by: Rick Gabriel
Email address: klaxian@gmail.com
PostgreSQL version: 10.4
Operating system: Ubuntu Linux 16.04
Description:
When using COALESCE with an aggregate subquery to a CTE, NULL is returned
instead of the first non-null value in the argument list. Proof-of-concept
examples are below. I would expect COALESCE to return 0 in these cases. Am I
missing something? Thanks.
WITH test_cte AS (SELECT 1 AS id, 2 AS qty)
SELECT COALESCE(SUM(qty), 0) FROM test_cte WHERE id=2;
WITH test_cte AS (SELECT 1 AS id, 2 AS qty)
SELECT COALESCE((SELECT SUM(qty) FROM test_cte WHERE id=2), 0);