Received: from postgresql.org.org (webmail.postgresql.org [216.126.85.28]) by postgresql.org (8.11.3/8.11.1) with SMTP id f5MCbva48686 for ; Fri, 22 Jun 2001 08:37:57 -0400 (EDT) (envelope-from pgsql-sql-owner@postgresql.org) Received: from news.tht.net (news.hub.org [216.126.91.242]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f5M8Vea47623 for ; Fri, 22 Jun 2001 04:31:40 -0400 (EDT) (envelope-from news@news.tht.net) Received: (from news@localhost) by news.tht.net (8.11.4/8.11.4) id f5M8GJI35186 for pgsql-sql@postgresql.org; Fri, 22 Jun 2001 04:16:19 -0400 (EDT) (envelope-from news) From: domingo@dad-it.com (Domingo Alvarez Duarte) X-Newsgroups: comp.databases.postgresql.sql Subject: Incremental sum ? Date: 22 Jun 2001 01:16:15 -0700 Organization: http://groups.google.com/ Lines: 27 Message-ID: <70a76315.0106220016.2a3a6f0b@posting.google.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Complaints-To: groups-support@google.com To: pgsql-sql@postgresql.org X-Archive-Number: 200106/365 X-Sequence-Number: 3655 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 ?