BUG #15208: COALESCE with CTE returns NULL - Mailing list pgsql-bugs

From PG Bug reporting form
Subject BUG #15208: COALESCE with CTE returns NULL
Date
Msg-id 152726503844.26748.4906459255150532845@wrigleys.postgresql.org
Whole thread Raw
Responses Re: BUG #15208: COALESCE with CTE returns NULL
Re: BUG #15208: COALESCE with CTE returns NULL
List pgsql-bugs
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);


pgsql-bugs by date:

Previous
From: Magnus Hagander
Date:
Subject: Re: PostgreSQL 2018-05-10 Security Update Release
Next
From: Sergei Kornilov
Date:
Subject: Re: BUG #15208: COALESCE with CTE returns NULL