Re: Sum() rows

Поиск
Список
Период
Сортировка
От Bruno Wolff III
Тема Re: Sum() rows
Дата
Msg-id 20050531171708.GB5899@wolff.to
обсуждение исходный текст
Ответ на Sum() rows  (lucas@presserv.org)
Ответы Re: Sum() rows
Список pgsql-sql
On Tue, May 31, 2005 at 13:36:48 -0300, lucas@presserv.org wrote:
> Hi.
> How can I sum a row and show the sum for each row???

Since in your example the id field gives the ordering, you can use a subselect
to add up the subtotal for rows with and id less than or equal to the value
of id for the current row.

> For example, in a finances table that have the total movimentation(debit/credit)
> in the bank.
> 
> i.e:
> CREATE TABLE TB1 (id integer primary key, value numeric);
> insert into tb1 values (1,20);
> insert into tb1 values (2,2);
> insert into tb1 values (3,3);
> insert into tb1 values (4,17);
> insert into tb1 values (5,-0.5);
> insert into tb1 values (6,3);
> 
> I want a query that returns:
> -id- | --- value --- | --- subtot ---
>    1 |        20.00  |         20.00
>    2 |         2.00  |         22.00
>    3 |         3.00  |         25.00
>    4 |        17.00  |         42.00
>    5 |        -0.50  |         41.50
>    6 |         3.00  |         44.50
> 
> The subtot colum will be the "prev. subtot colum"+"value colum". :-/
> I dont know how to make the "subtot" colum, I tried to use the sum() function
> but it not works correctly.
> Any idea???
> 
> Thanks.
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
> 
>                http://www.postgresql.org/docs/faq


В списке pgsql-sql по дате отправления:

Предыдущее
От: lucas@presserv.org
Дата:
Сообщение: Sum() rows
Следующее
От: "scm-master"
Дата:
Сообщение: Управление Цепями Поставок, мастер-класс