Received: from ece.rice.edu (ece.rice.edu [128.42.4.34]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f5MHIZa90117 for ; Fri, 22 Jun 2001 13:18:35 -0400 (EDT) (envelope-from reedstrm@rice.edu) Received: from wallace.ece.rice.edu (wallace.ece.rice.edu [128.42.12.154]) by ece.rice.edu (Postfix) with ESMTP id 0448668A0E; Fri, 22 Jun 2001 12:18:33 -0500 (CDT) Received: from reedstrm by wallace.ece.rice.edu with local (Exim 3.22 #1 (Debian)) id 15DUZV-0003kp-00; Fri, 22 Jun 2001 12:18:17 -0500 Date: Fri, 22 Jun 2001 12:18:16 -0500 From: "Ross J. Reedstrom" To: Tom Lane Cc: Alex Pilosov , Domingo Alvarez Duarte , pgsql-sql@postgresql.org Subject: Re: Incremental sum ? Message-ID: <20010622121816.B14381@rice.edu> Mail-Followup-To: "Ross J. Reedstrom" , Tom Lane , Alex Pilosov , Domingo Alvarez Duarte , pgsql-sql@postgresql.org References: <20010622112119.D13451@rice.edu> <4526.993229126@sss.pgh.pa.us> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4526.993229126@sss.pgh.pa.us> User-Agent: Mutt/1.3.18i X-Archive-Number: 200106/382 X-Sequence-Number: 3678 On Fri, Jun 22, 2001 at 12:58:46PM -0400, Tom Lane wrote: > "Ross J. Reedstrom" writes: > > And here's the working example: not the need to GROUP BY, and <= > > to get the current payment. > > > select cust_id,invoice_id,val,paid, (select (sum(val) - sum(paid)) > > from invoices_not_paid where cust_id= i.cust_id and invoice_id <= > > i.invoice_id group by cust_id) as balance from invoices_not_paid i; > > Actually I think you could leave off the inner GROUP BY --- won't there > always be exactly one group, since only one value of inner cust_id is > selected? Sure enough, it works fine. My internal rule: "can't use aggregates without a group by" mis-fired. Ross