pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alex Pilosov <alex@pilosoft.com>
To: Domingo Alvarez Duarte <domingo@dad-it.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Incremental sum ?
Date: Fri, 22 Jun 2001 11:29:25 -0400 (EDT)
Message-ID: <Pine.BSO.4.10.10106221127520.17823-100000@spider.pilosoft.com> (raw)
In-Reply-To: <70a76315.0106220016.2a3a6f0b@posting.google.com>

It should be done using subqueries.
select ..., (
   select sum(val)-sum(paid) from invoices i2
   where i2.invoice_id<i.invoice_id
     and i2.cust_id=i.cust_id
   )
from invoices i


On 22 Jun 2001, Domingo Alvarez Duarte wrote:

> 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
> 
> 




view thread (5+ messages)  latest in thread

Message-ID: <Pine.BSO.4.10.10106221127520.17823-100000@spider.pilosoft.com>
Permalink:  ../Pine.BSO.4.10.10106221127520.17823-100000@spider.pilosoft.com/
Also on:    postgresql.org/message-id/Pine.BSO.4.10.10106221127520.17823-100000@spider.pilosoft.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: alex@pilosoft.com, domingo@dad-it.com
  Subject: Re: Incremental sum ?
  In-Reply-To: <Pine.BSO.4.10.10106221127520.17823-100000@spider.pilosoft.com>
  References: <70a76315.0106220016.2a3a6f0b@posting.google.com> <Pine.BSO.4.10.10106221127520.17823-100000@spider.pilosoft.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox