Received: from spider.pilosoft.com (p55-222.acedsl.com [160.79.55.222]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f5MFJUa33744 for ; Fri, 22 Jun 2001 11:19:31 -0400 (EDT) (envelope-from alex@pilosoft.com) Received: from localhost (alexmail@localhost) by spider.pilosoft.com (8.9.3/8.9.3) with ESMTP id LAA17465; Fri, 22 Jun 2001 11:29:25 -0400 (EDT) Date: Fri, 22 Jun 2001 11:29:25 -0400 (EDT) From: Alex Pilosov To: Domingo Alvarez Duarte cc: pgsql-sql@postgresql.org Subject: Re: Incremental sum ? In-Reply-To: <70a76315.0106220016.2a3a6f0b@posting.google.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Archive-Number: 200106/373 X-Sequence-Number: 3664 It should be done using subqueries. select ..., ( select sum(val)-sum(paid) from invoices i2 where i2.invoice_id I have a problem that requires what I call a incremental sum, lets say > I have the folowing table (for simplicity): > > table invoices_not_paid(cust_id int, invoice_id int, val numeric, paid > numeric); > > with the folowing values: > > cust_id invoice_id val paid > ---------------------------------- > 1 23 10.50 3.40 > 1 34 5.70 0.0 > 1 67 23.89 4.50 > > > I want show a list like this: > > cust_id invoice_id val paid incremental_not_paid_sum > ----------------------------------------------------------------- > 1 23 10.50 3.40 (10.50 - 3.40) 7.10 > 1 34 5.70 0.0 (7.10 + 5.70 - 0.0) 12.80 > 1 67 23.89 4.50 (12.80 + 23.89 - 4.50) 31.19 > > The operations betwen () are showed only to explain how the > incremental_not_paid_sum is calculated, The operation requires a > reference to a previous column or a partial sum of columns till that > moment, someone has an idea how this can be done using sql ? > > ---------------------------(end of broadcast)--------------------------- > TIP 4: Don't 'kill -9' the postmaster > >