pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Joel Mc Graw <jpmcgraw1@home.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: 7.0.3 and 7.1.3 different results?
Date: Tue, 02 Oct 2001 10:56:11 -0400
Message-ID: <8826.1002034571@sss.pgh.pa.us> (raw)
In-Reply-To: <3BB8DCBB.8C96D5AC@home.com>
References: <3BB8DCBB.8C96D5AC@home.com>

Joel Mc Graw <jpmcgraw1@home.com> writes:
> A query such as "select sum(pages) from job_documents where
> delivery_type='print'" returned 0 in version 7.0.3 if there were no rows
> matching the query.  In 7.1.3 the result is NULL if no rows match the
> query.  Why the change?  Which result is "correct" according to the SQL
> standard?

NULL is correct according to the SQL standard (yes, I think it's
brain-dead too).  SQL92 section 6.5 saith:

            b) If AVG, MAX, MIN, or SUM is specified, then

              Case:

              i) If TXA is empty, then the result is the null value.
                 --------------------------------------------------

             ii) If AVG is specified, then the result is the average of the
                 values in TXA.

            iii) If MAX or MIN is specified, then the result is respec-
                 tively the maximum or minimum value in TXA. These results
                 are determined using the comparison rules specified in
                 Subclause 8.2, "<comparison predicate>".

             iv) If SUM is specified, then the result is the sum of the
                 values in TXA. If the sum is not within the range of the
                 data type of the result, then an exception condition is
                 raised: data exception-numeric value out of range.

I'd suggest COALESCE(SUM(foo), 0) if you need a zero result.

However, I dispute your assertion that 7.0.3 returned zero.  I just
double checked, and I get a NULL there too.  If we ever returned zero,
it was a long time ago.

			regards, tom lane



view thread (5+ messages)  latest in thread

Message-ID: <8826.1002034571@sss.pgh.pa.us>
Permalink:  ../8826.1002034571@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/8826.1002034571@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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, jpmcgraw1@home.com
  Subject: Re: 7.0.3 and 7.1.3 different results?
  In-Reply-To: <8826.1002034571@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