Re: How to obtain algebraic sum of equal lines

From: Oscar Rodriguez Fonseca <info(at)vraniscci(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Cc: nasr(dot)laili(at)tin(dot)it
Subject: Re: How to obtain algebraic sum of equal lines
Date: 2006-04-26 14:25:55
Message-ID: 20060426162555.233c2d2d@vrlap.localvrnet
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Tue, 25 Apr 2006 22:12:40 +0200
Ennio-Sr <nasr(dot)laili(at)tin(dot)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

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2006-04-26 16:38:12 Re: Perl and psql variables
Previous Message Sean Davis 2006-04-26 13:04:44 Re: Perl and psql variables