Re: [GENERAL] CASE/WHEN behavior with NULLS

2012-09-02 Thread Craig Ringer
On 09/01/2012 02:07 PM, Thalis Kalfigkopoulos wrote: Thanks all for the replies. Actually I had already tested that sum() behaved correctly with respect to NULLs, meaning that it ignored them (or treated them as 0, couldn't really tell). That's why I went ahead sum()ing even though I knew NULLs w

Re: [GENERAL] CASE/WHEN behavior with NULLS

2012-09-02 Thread Jasen Betts
On 2012-09-01, Chris Angelico wrote: > On Sat, Sep 1, 2012 at 12:07 PM, David Johnston wrote: >> 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 su

Re: [GENERAL] CASE/WHEN behavior with NULLS

2012-09-01 Thread Thalis Kalfigkopoulos
On Sat, Sep 1, 2012 at 6:19 AM, David Johnston wrote: > On Aug 31, 2012, at 22:49, Tom Lane wrote: > >> David Johnston writes: >>> On Aug 31, 2012, at 21:52, Tom Lane wrote: David Johnston writes: > That said you might want to try > SUM(COALESCE(foo, 0)) >> Actually I'd go wi

Re: [GENERAL] CASE/WHEN behavior with NULLS

2012-08-31 Thread David Johnston
On Aug 31, 2012, at 22:49, Tom Lane wrote: > David Johnston writes: >> On Aug 31, 2012, at 21:52, Tom Lane wrote: >>> David Johnston 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 COAL

Re: [GENERAL] CASE/WHEN behavior with NULLS

2012-08-31 Thread Tom Lane
David Johnston writes: > On Aug 31, 2012, at 21:52, Tom Lane wrote: >> David Johnston 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 equiv

Re: [GENERAL] CASE/WHEN behavior with NULLS

2012-08-31 Thread Chris Angelico
On Sat, Sep 1, 2012 at 12:07 PM, David Johnston wrote: > 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. It comes to the same result with SU

Re: [GENERAL] CASE/WHEN behavior with NULLS

2012-08-31 Thread David Johnston
On Aug 31, 2012, at 21:52, Tom Lane wrote: > David Johnston writes: >> On Aug 31, 2012, at 19:14, Thalis Kalfigkopoulos 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 >

Re: [GENERAL] CASE/WHEN behavior with NULLS

2012-08-31 Thread Tom Lane
David Johnston writes: > On Aug 31, 2012, at 19:14, Thalis Kalfigkopoulos 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

Re: [GENERAL] CASE/WHEN behavior with NULLS

2012-08-31 Thread David Johnston
On Aug 31, 2012, at 19:14, Thalis Kalfigkopoulos wrote: > Hello all, > > I have a query that presents a sum() where in some records it's NULL > because all members of the group are NULL. > I decided I wanted to see a pretty 0 instead of NULL since it fits the > logic of the app. > > This didn't

[GENERAL] CASE/WHEN behavior with NULLS

2012-08-31 Thread Thalis Kalfigkopoulos
Hello all, I have a query that presents a sum() where in some records it's NULL because all members of the group are NULL. I decided I wanted to see a pretty 0 instead of NULL since it fits the logic of the app. This didn't work as expected (the NULL's persisted): ...CASE sum(foo) WHEN NULL THEN