pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Benjamin Smith <bens@effortlessis.com>
To: pgsql-sql@postgresql.org
Cc: Benjamin Smith <bens@effortlessis.com>
Subject: Accumulative Queries?
Date: Sat, 30 Nov 2002 14:40:08 PST
Message-ID: <200211302240.gAUMe8c25335@host.effortlessis.com> (raw)

Let's say you have a table of Financial transactions: 

Create table checks ( 
id serial, 
number varchar, 
to varchar, 
amount real, 
date integer 
); 

(date is an epoch timestamp) 

And you want to get a listing of checks 

"SELECT * FROM checks ORDER BY date ASC"; 

but you also want to have an accumulative field that adds up the amount field as the results are returned, so you might see results like: 

id number to amount date balance
1 0 Deposit -100 12344 100
2 100 Jack 40 123455 60
3 101 Bob 20 123345 40 
4 102 VOID 0 0 40
5 103 Harold 11 123488 29

Is this possible using only SQL? 

Also, assuming you have checks year round, how might you get results only in March that have totals consistent for the time frame while taking into account all the other checks in Jan and Feb? 

- The best way to predict the future is to invent it. -







view thread (2+ messages)  latest in thread

Message-ID: <200211302240.gAUMe8c25335@host.effortlessis.com>
Permalink:  ../200211302240.gAUMe8c25335@host.effortlessis.com/
Also on:    postgresql.org/message-id/200211302240.gAUMe8c25335@host.effortlessis.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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: bens@effortlessis.com
  Subject: Re: Accumulative Queries?
  In-Reply-To: <200211302240.gAUMe8c25335@host.effortlessis.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