pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Re: Accumulated sums in SQL query
3+ messages / 2 participants
[nested] [flat]

* Re: Accumulated sums in SQL query
@ 2002-10-28 08:20 Christoph Haller <ch@rodos.fzk.de>
  0 siblings, 0 replies; 3+ messages in thread

From: Christoph Haller @ 2002-10-28 08:20 UTC (permalink / raw)
  To: kabai@audiobox.hu; +Cc: pgsql-sql

> Which is the simplest way to create an SQL query to get accumulated
sums of
>  records like this (from the table containing the numbers):
>
> number    sums
> ---------------
> 1               1
> 2               3
> 3               6
> 4              10
>
SELECT number, SUM(your_sum_column) FROM your_table
GROUP BY number ;

Regards, Christoph




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Accumulated sums in SQL query
@ 2002-10-28 12:27 Christoph Haller <ch@rodos.fzk.de>
  2002-10-28 14:57 ` Re: Accumulated sums in SQL query Marek Bartnikowski <marek@easy.eu.org>
  0 siblings, 1 reply; 3+ messages in thread

From: Christoph Haller @ 2002-10-28 12:27 UTC (permalink / raw)
  To: kabai@audiobox.hu; +Cc: pgsql-sql

>
> OK I have a table named bank_account_movements containing two columns
=
> date and amount:
>
> date                               amount (in USD)
> -------------------------------------
> 2002-10-01                   20
> 2002-10-02                   30
> 2002-10-03                   -15
> 2002-10-04                   -5
> 2002-10-05                  -3
> 2002-10-06                    10
>
> my goal is to create a view from it adding an extra column named =
> balance!
>
> date                               amount (in USD)      balance
> -----------------------------------------------------------
> 2002-10-01                   20                             20
> 2002-10-02                   30                             50
> 2002-10-03                   -15                            35
> 2002-10-04                   -5                              30
> 2002-10-05                  -3                               27
> 2002-10-06                    10                             17
>
> The balance is 0+20=20, 0+20+30=50, 0+20+30-15=35 and so on...
> how would you write the SQL query?
>

My first approach is write a small plpgsql function
(based on the table definition below) like

CREATE TABLE amountlist (date TIMESTAMP,amount INTEGER);
INSERT INTO  amountlist VALUES ('2002-10-01 00:00:00', 20 ) ;
INSERT INTO  amountlist VALUES ('2002-10-02 00:00:00', 30 ) ;
INSERT INTO  amountlist VALUES ('2002-10-03 00:00:00',-15 ) ;
INSERT INTO  amountlist VALUES ('2002-10-04 00:00:00', -5 ) ;
INSERT INTO  amountlist VALUES ('2002-10-05 00:00:00', -3 ) ;
INSERT INTO  amountlist VALUES ('2002-10-06 00:00:00', 10 ) ;
CREATE FUNCTION calc_balance(TIMESTAMP) RETURNS INTEGER AS '
DECLARE balance INTEGER;
BEGIN
SELECT INTO balance SUM(amount) FROM amountlist WHERE date <= $1 ;
RETURN balance;
END;
' LANGUAGE 'plpgsql' ;

SELECT date,amount,calc_balance(date) FROM amountlist;
          date          | amount | calc_balance
------------------------+--------+--------------
 2002-10-01 00:00:00+02 |     20 |           20
 2002-10-02 00:00:00+02 |     30 |           50
 2002-10-03 00:00:00+02 |    -15 |           35
 2002-10-04 00:00:00+02 |     -5 |           30
 2002-10-05 00:00:00+02 |     -3 |           27
 2002-10-06 00:00:00+02 |     10 |           37
(6 rows)

Looks like what you are looking for, except the last value which
appears to be a typo.

Regards, Christoph





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Accumulated sums in SQL query
  2002-10-28 12:27 Re: Accumulated sums in SQL query Christoph Haller <ch@rodos.fzk.de>
@ 2002-10-28 14:57 ` Marek Bartnikowski <marek@easy.eu.org>
  0 siblings, 0 replies; 3+ messages in thread

From: Marek Bartnikowski @ 2002-10-28 14:57 UTC (permalink / raw)
  To: pgsql-sql

I think,that is not good solution. What will happen when some records
have the same date? I suggest to operate on rowid or something like
thaĊ£ (maybe some serial?) and everything will be good.
But, when each row has different date, its good :)
regards.
marek

On Mon, Oct 28, 2002 at 03:55:46PM +0100, Kabai J?zsef (I don't know why;) wrote:
: Thank you Christoph this logic helped me a lot.
: Regards Joseph
: ----- Original Message ----- 
: From: "Christoph Haller" <ch@rodos.fzk.de>
: To: <kabai@audiobox.hu>
: Cc: <pgsql-sql@postgresql.org>
: Sent: Monday, October 28, 2002 1:27 PM
: Subject: Re: [SQL] Accumulated sums in SQL query
: 
: 
: > >
: > > OK I have a table named bank_account_movements containing two columns
: > =
: > > date and amount:
: > >
: > > date                               amount (in USD)
: > > -------------------------------------
: > > 2002-10-01                   20
: > > 2002-10-02                   30
: > > 2002-10-03                   -15
: > > 2002-10-04                   -5
: > > 2002-10-05                  -3
: > > 2002-10-06                    10
: > >
: > > my goal is to create a view from it adding an extra column named =
: > > balance!
: > >
: > > date                               amount (in USD)      balance
: > > -----------------------------------------------------------
: > > 2002-10-01                   20                             20
: > > 2002-10-02                   30                             50
: > > 2002-10-03                   -15                            35
: > > 2002-10-04                   -5                              30
: > > 2002-10-05                  -3                               27
: > > 2002-10-06                    10                             17
: > >
: > > The balance is 0+20=20, 0+20+30=50, 0+20+30-15=35 and so on...
: > > how would you write the SQL query?
: > >
: > 
: > My first approach is write a small plpgsql function
: > (based on the table definition below) like
: > 
: > CREATE TABLE amountlist (date TIMESTAMP,amount INTEGER);
: > INSERT INTO  amountlist VALUES ('2002-10-01 00:00:00', 20 ) ;
: > INSERT INTO  amountlist VALUES ('2002-10-02 00:00:00', 30 ) ;
: > INSERT INTO  amountlist VALUES ('2002-10-03 00:00:00',-15 ) ;
: > INSERT INTO  amountlist VALUES ('2002-10-04 00:00:00', -5 ) ;
: > INSERT INTO  amountlist VALUES ('2002-10-05 00:00:00', -3 ) ;
: > INSERT INTO  amountlist VALUES ('2002-10-06 00:00:00', 10 ) ;
: > CREATE FUNCTION calc_balance(TIMESTAMP) RETURNS INTEGER AS '
: > DECLARE balance INTEGER;
: > BEGIN
: > SELECT INTO balance SUM(amount) FROM amountlist WHERE date <= $1 ;
: > RETURN balance;
: > END;
: > ' LANGUAGE 'plpgsql' ;
: > 
: > SELECT date,amount,calc_balance(date) FROM amountlist;
: >           date          | amount | calc_balance
: > ------------------------+--------+--------------
: >  2002-10-01 00:00:00+02 |     20 |           20
: >  2002-10-02 00:00:00+02 |     30 |           50
: >  2002-10-03 00:00:00+02 |    -15 |           35
: >  2002-10-04 00:00:00+02 |     -5 |           30
: >  2002-10-05 00:00:00+02 |     -3 |           27
: >  2002-10-06 00:00:00+02 |     10 |           37
: > (6 rows)
: > 
: > Looks like what you are looking for, except the last value which
: > appears to be a typo.
: > 
: > Regards, Christoph
: > 
: > 
: > 
: > ---------------------------(end of broadcast)---------------------------
: > TIP 3: if posting/reading through Usenet, please send an appropriate
: > subscribe-nomail command to majordomo@postgresql.org so that your
: > message can get through to the mailing list cleanly
: > 
: 
: 
: 
: ---------------------------(end of broadcast)---------------------------
: TIP 2: you can get off all lists at once with the unregister command
:     (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)

-- 
Marek Bartnikowski       http://easy.eu.org
 * It is more complicated than you think *



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2002-10-28 14:57 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-10-28 08:20 Re: Accumulated sums in SQL query Christoph Haller <ch@rodos.fzk.de>
2002-10-28 12:27 Re: Accumulated sums in SQL query Christoph Haller <ch@rodos.fzk.de>
2002-10-28 14:57 ` Marek Bartnikowski <marek@easy.eu.org>

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