Received: from sss.pgh.pa.us ([192.204.191.242]) by postgresql.org (8.11.3/8.11.4) with ESMTP id f92EuKn79182 for ; Tue, 2 Oct 2001 10:56:20 -0400 (EDT) (envelope-from tgl@sss.pgh.pa.us) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.11.4/8.11.4) with ESMTP id f92EuBI08829; Tue, 2 Oct 2001 10:56:11 -0400 (EDT) To: Joel Mc Graw cc: pgsql-sql@postgresql.org Subject: Re: 7.0.3 and 7.1.3 different results? In-reply-to: <3BB8DCBB.8C96D5AC@home.com> References: <3BB8DCBB.8C96D5AC@home.com> Comments: In-reply-to Joel Mc Graw message dated "Mon, 01 Oct 2001 14:14:35 -0700" Date: Tue, 02 Oct 2001 10:56:11 -0400 Message-ID: <8826.1002034571@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200110/15 X-Sequence-Number: 4970 Joel Mc Graw 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, "". 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