pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-sql@hub.org
Subject: Re: sum gives different answer
Date: Sun, 14 Mar 1999 13:04:27 -0500
Message-ID: <29740.921434667@sss.pgh.pa.us> (raw)
In-Reply-To: <199903140400.XAA11118@hub.org>

Chairudin Sentosa <chairudin@prima.net.id> writes:
> I have two SQL statements that I expect to give (0 rows) as output.
> However the first one with 'sum' does not do that.

> select pin, sum(duration) from tablename where date(start_time)=
> (select date 'today') group by pin;
> pin|sum
> - ---+---
>    |
> (1 row)

> select pin, duration from tablename where date(start_time)=
> (select date 'today') group by pin;
> pin|duration
> - ---+--------
> (0 rows)

As George Moga pointed out, SUM() applied to an empty collection of tuples
still gives a result (one tuple containing NULL).  Aggregates in general
will give a result of some kind for an empty input collection --- for
example, you'd surely want COUNT() to return 0 not nothing.

However what you're talking about here is different: there are no groups
in the result, therefore SUM() should never have been applied at all,
not applied once to an empty set of tuples.  I'm firmly of the opinion
that the first example above is a bug.  The hackers list has been around
on this question a couple of times, and there are some folks who claim
that the current behavior is OK, but I'm at a loss to follow their
reasoning.

			regards, tom lane



view thread (6+ messages)  latest in thread

Message-ID: <29740.921434667@sss.pgh.pa.us>
Permalink:  ../29740.921434667@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/29740.921434667@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, pgsql-sql@hub.org
  Subject: Re: sum gives different answer
  In-Reply-To: <29740.921434667@sss.pgh.pa.us>
  References: <199903140400.XAA11118@hub.org> <29740.921434667@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox