Re: Re: [SQL] variables in SQL??

From: Craig Johannsen <cjohan(at)home(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Cc: ian(dot)schumacher(at)telus(dot)net, Francisco Hernandez <xy0xy0(at)earthlink(dot)net>, pgsql-general(at)postgresql(dot)org
Subject: Re: Re: [SQL] variables in SQL??
Date: 2000-08-18 06:12:44
Message-ID: 399CD3DC.F028B264@home.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

You can create a running total provided that you have a unique
sequentially increasing (or decreasing) ID for each row. See the
following example:

create table tran(id int primary key, price dec(8,2));
insert into tran values(1,5.00);
insert into tran values(2,4.00);
insert into tran values(3,10.00);
insert into tran values(4,2.00);
insert into tran values(5,7.00);

select price, (select sum(price) from tran as d1
where d1.id <= d2.id) as "sum"
from tran as d2;

price | sum
-------+-------
5.00 | 5.00
4.00 | 9.00
10.00 | 19.00
2.00 | 21.00
7.00 | 28.00
(5 rows)

Francisco wrote:

> > what im trying to do is have a Sum of a colum.. as it goes forwards with the
> > cursor..
> > like so:
> >
> > Price|Sum
> > 5|5
> > 4|9
> > 10|19
> > 2|21
> > 7|28
>

====================================
Craig Johannsen
Critical Path Consulting, Inc.
604-762-1514
http://members.home.net/cjohan/cpath
====================================

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Carl Alexander 2000-08-18 06:49:08 New user question -- alternate programmer interfaces?
Previous Message AlphaByte 2000-08-18 05:02:34 I screwed it up, my installation :(

Browse pgsql-sql by date

  From Date Subject
Next Message Joerg Hessdoerfer 2000-08-18 07:59:42 Re: Continuous inserts...
Previous Message brianb-pgsql 2000-08-18 01:41:20 Re: Continuous inserts...