Accumulative Queries? - Mailing list pgsql-sql

From Benjamin Smith
Subject Accumulative Queries?
Date
Msg-id 200211302240.gAUMe8c25335@host.effortlessis.com
Whole thread Raw
Responses Re: Accumulative Queries?  (Joel Burton <joel@joelburton.com>)
List 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
seeresults 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
timeframe while taking into account all the other checks in Jan and Feb? 
 

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






pgsql-sql by date:

Previous
From: Mike Winter
Date:
Subject: Question about slow Select when using 'IN'.
Next
From: Evgen Potemkin
Date:
Subject: SQL99