Accumulative Queries?

From: Benjamin Smith <bens(at)effortlessis(dot)com>
To: <pgsql-sql(at)postgresql(dot)org>
Cc: Benjamin Smith <bens(at)effortlessis(dot)com>
Subject: Accumulative Queries?
Date: 2002-11-30 14:40:08
Message-ID: 200211302240.gAUMe8c25335@host.effortlessis.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Rajesh Kumar Mallah. 2002-11-30 19:06:32 How to take advantage of PREPARED QUERIES of PGSQL 7.3 in mod_perl enviornment?
Previous Message A.M. 2002-11-30 04:31:27 Re: