Search Postgresql Archives

Re: CASE/WHEN behavior with NULLS

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Aug 31, 2012, at 22:49, Tom Lane <tgl@xxxxxxxxxxxxx> wrote:

> David Johnston <polobo@xxxxxxxxx> writes:
>> On Aug 31, 2012, at 21:52, Tom Lane <tgl@xxxxxxxxxxxxx> wrote:
>>> David Johnston <polobo@xxxxxxxxx> writes:
>>>> 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.
> 
>> These are not equivalent if some values of foo are not-null and you want the sum of all non-null values while replacing any nulls with zero.  So the decision depends on what and why you are summing. 
> 
> But SUM() ignores input nulls, so I think they really are equivalent.
> I agree that in a lot of other cases (for instance MAX), you'd have to
> think harder about which behavior you wanted.
> 

This I did not know/recall, was assuming nulls poisoned the result.

David J.

-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux