Re: How to obtain algebraic sum of equal lines - Mailing list pgsql-novice

From Oscar Rodriguez Fonseca
Subject Re: How to obtain algebraic sum of equal lines
Date
Msg-id 20060426162555.233c2d2d@vrlap.localvrnet
Whole thread Raw
In response to How to obtain algebraic sum of equal lines  (Ennio-Sr <nasr.laili@tin.it>)
List pgsql-novice
On Tue, 25 Apr 2006 22:12:40 +0200
Ennio-Sr <nasr.laili@tin.it> wrote:

> Is it possible, given the following table:

> cod_rif | titolo | quantity |   cmf   |  u_qq  |  mont   | vend |  sum
> ---------+--------+----------+---------+--------+---------+------+-------
>  26      | aaa    |    -1000 | 6.11098 |  6.176 |  6.1110 | t    | -1000
>  7       | aaa    |     2500 |  25.455 |      0 | 60.0897 | f    |  2500
>  28      | bbb    |     2700 |   3.862 |    4.6 |  4.1957 | f    |  2700
[...]
> (10 rows)
> to get a selection whereby the algebraic sum of "quantity" for each equal 'titolo'
> is returned?

I am no expert but maybe you could try using "join":

SELECT t.*,s.sum AS quantity_sum FROM test_t AS t \


INNER JOIN (SELECT titolo,sum(quantity) FROM test_t GROUP BY titolo) AS s \
ON t.titolo = s.titolo ORDER BY t.titolo ASC

This (untested) command will just add a column quantity_sum to the result table.

Regards,

--
Oscar

pgsql-novice by date:

Previous
From: Sean Davis
Date:
Subject: Re: Perl and psql variables
Next
From: Tom Lane
Date:
Subject: Re: Perl and psql variables