Received: from mx99.openu.ac.il (arbel.openu.ac.il [147.233.32.4]) by hub.org (8.9.3/8.9.1) with ESMTP id NAA08084 for ; Sun, 25 Apr 1999 13:27:39 -0400 (EDT) (envelope-from herouth@oumail.openu.ac.il) Received: from talmon.openu.ac.il (talmon.openu.ac.il [147.233.32.11]) by mx99.openu.ac.il (Postfix) with ESMTP for id 137DC4B581; Sun, 25 Apr 1999 20:27:36 +0300 (IDT) Received: from [147.233.159.109] (mac-herouth.pc.openu.ac.il) by talmon.openu.ac.il (Netscape Messaging Server 3.54) with ESMTP id AAA4808; Sun, 25 Apr 1999 20:29:42 +0300 X-Sender: herouth@shaked.cc.openu.ac.il Message-Id: In-Reply-To: <10830.925058444@sss.pgh.pa.us> References: Your message of Sun, 25 Apr 1999 17:25:14 +0300 Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Date: Sun, 25 Apr 1999 20:27:55 +0300 To: Tom Lane From: Herouth Maoz Subject: Re: [INTERFACES] JDBC next() method Cc: pgsql-interfaces@postgresql.org At 19:40 +0300 on 25/04/1999, Tom Lane wrote: > Well, this is certainly adequate precedent for the behavior of these > particular aggregates --- although I'd have to say that the standard- > writers blew it for SUM; SUM of an empty set ought to return 0 not > null. (It looks like Postgres follows the spec, however.) I don't agree. Suppose you want to sum of all your banking transactions in January. There is a distinction between getting a 0, meaning you had a balanced budget in January, and getting a null, meaning you made no transactions in January. > Now that I think about it, the arguments on the hackers list were not > about the plain SELECT case but about the GROUP BY case. For example, > if you do > > SELECT productname, AVG(saleprice) FROM sales GROUP BY productname; > > then you get a row in the output for each different productname, and > a separate instance of AVG is run over the prices for each group. > (Unless there are NULLs in the saleprice column, none of the AVG > instances could ever return a null result.) > > BUT: what happens if the sales table is empty? There are no > productnames, therefore no groups, therefore no rows ought to appear > in the output (IMHO). However, what Postgres actually does right now > is to emit one all-nulls row (but only if an aggregate function was > used; if you say "SELECT productname FROM sales GROUP BY productname" > then you get no rows). That is the behavior that we've gone 'round and > 'round on without any resolution; it seems obviously inconsistent to me, > but others think it's OK because it parallels what happens in the non- > GROUP BY case. > > Is there anything in the SQL92 spec addressing this point? Ooh, definitely... Here are the general rules regarding a query expression (i.e. a general select statement). A grouped table is defined somewhere else in the document as the result of a group by or having clause. You really should read the definitions (I can make my draft available on the web for a while. I don't remember where I downloaded it). <<< Begin quotation >>> General Rules 1) Case: a) If T is not a grouped table, then Case: i) If the does not include a that contains a reference to T, then each is applied to each row of T yielding a table of M rows, where M is the cardinality of T. The i-th column of the table contains the values derived by the evaluation of the i-th . Case: 1) If the DISTINCT is not specified, then the table is the result of the . 2) If the DISTINCT is specified, then the result of the is the table derived from that table by the elimination of any redundant duplicate rows. b) If T is a grouped table, then Case: i) If T has 0 groups, then the result of the is an empty table. ii) If T has one or more groups, then each is applied to each group of T yielding a table of M rows, where M is the number of groups in T. The i-th column of the table contains the values derived by the evaluation of the i-th . When a is applied to a given group of T, that group is the argument or argument source of each in the . Case: 1) If the DISTINCT is not specified, then the table is the result of the . 2) If the DISTINCT is specified, then the result of the is the table derived from T by the elimination of any redundant duplicate rows. <<< End quotation >>> Which implies exactly what you say. Perhaps you should forward this to the Hackers list to re-start the argument - I am not a subscriber there. Herouth -- Herouth Maoz, Internet developer. Open University of Israel - Telem project http://telem.openu.ac.il/~herutma