BUG #15208: COALESCE with CTE returns NULL

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: klaxian(at)gmail(dot)com
Subject: BUG #15208: COALESCE with CTE returns NULL
Date: 2018-05-25 16:17:18
Message-ID: 152726503844.26748.4906459255150532845@wrigleys.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 15208
Logged by: Rick Gabriel
Email address: klaxian(at)gmail(dot)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);

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Sergei Kornilov 2018-05-25 16:24:42 Re: BUG #15208: COALESCE with CTE returns NULL
Previous Message Magnus Hagander 2018-05-25 08:16:20 Re: PostgreSQL 2018-05-10 Security Update Release