Received: from sss.pgh.pa.us ([192.204.191.242]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f5MH0Pa82273 for ; Fri, 22 Jun 2001 13:00:25 -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 f5MGwl304529; Fri, 22 Jun 2001 12:58:47 -0400 (EDT) To: "Ross J. Reedstrom" cc: Alex Pilosov , Domingo Alvarez Duarte , pgsql-sql@postgresql.org Subject: Re: Incremental sum ? In-reply-to: <20010622112119.D13451@rice.edu> References: <20010622112119.D13451@rice.edu> Comments: In-reply-to "Ross J. Reedstrom" message dated "Fri, 22 Jun 2001 11:21:19 -0500" Date: Fri, 22 Jun 2001 12:58:46 -0400 Message-ID: <4526.993229126@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200106/381 X-Sequence-Number: 3673 "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? regards, tom lane