Re: CASE/WHEN behavior with NULLS

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: David Johnston <polobo(at)yahoo(dot)com>
Cc: Thalis Kalfigkopoulos <tkalfigo(at)gmail(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: CASE/WHEN behavior with NULLS
Date: 2012-09-01 01:52:19
Message-ID: 28068.1346464339@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

David Johnston <polobo(at)yahoo(dot)com> writes:
> On Aug 31, 2012, at 19:14, Thalis Kalfigkopoulos <tkalfigo(at)gmail(dot)com> wrote:
>> This didn't work as expected (the NULL's persisted):
>> ...CASE sum(foo) WHEN NULL THEN 0 ELSE sum(foo) END...

> Guessing this form effectively evaluates to
> WHEN sum(foo) = NULL instead of IS NULL and thus the wrong answer:

Yeah, I think that's right.

> That said you might want to try
> SUM(COALESCE(foo, 0))

Actually I'd go with "COALESCE(SUM(foo), 0)" since that requires only
one COALESCE operation, not one per row.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David Johnston 2012-09-01 02:07:13 Re: CASE/WHEN behavior with NULLS
Previous Message David Johnston 2012-09-01 00:25:55 Re: CASE/WHEN behavior with NULLS