Re: [HACKERS] SUM() and GROUP BY - Mailing list pgsql-hackers

From Thomas G. Lockhart
Subject Re: [HACKERS] SUM() and GROUP BY
Date
Msg-id 369C3AC2.D8961D08@alumni.caltech.edu
Whole thread Raw
In response to SUM() and GROUP BY  ("D'Arcy" "J.M." Cain <darcy@druid.net>)
Responses Re: [HACKERS] SUM() and GROUP BY  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
> Does this seem right?
> druid=> SELECT client_id, SUM(tramount)
>         FROM acctrans GROUP BY client_id;
> client_id|sum
> ---------+---
>          |
> (1 row)
> If there are no rows in the table then shouldn't the result be no rows
> when GROUP BY is used?  Further, What about this?

Not sure. Someone may want to try this query on another DB. I know the
answer to the next one though...

> druid=> SELECT SUM(tramount) FROM acctrans;
> sum
> ---
> 
> (1 row)
> Shouldn't that be 0.00?

No. It is returning NULL, because NULL means "don't know". It doesn't
mean "nothing" or "zero". That is certainly the correct behavior if the
table were populated with all NULLs in that column. And by extension, it
is the correct result if there are no rows at all, since "don't know"
for a bunch should give the same result as "don't know" for a few or for
none.

> What will the NUMERIC or DECIMAL types do in these situations?  It
> looks like INTEGER has the same behaviour as MONEY (which tramount 
> is.)

They will all behave the same.
                   - Tom


pgsql-hackers by date:

Previous
From: Clark Evans
Date:
Subject: Re: [HACKERS] Re: EXCEPT/INTERSECT for v6.4
Next
From: "Thomas G. Lockhart"
Date:
Subject: Re: [HACKERS] Re: EXCEPT/INTERSECT for v6.4