Re: aggregate function ?

From: Raimon Fernandez <coder(at)montx(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: aggregate function ?
Date: 2007-05-15 23:38:49
Message-ID: AD84C6AE-DF17-454A-9F22-44C4D5DF85A9@montx.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Begin forwarded message:

> From: Raimon Fernandez <coder(at)montx(dot)com>
> Date: 16 maig 2007 01:04:10 GMT+02:00
> To: Richard Broersma Jr <rabroersma(at)yahoo(dot)com>
> Subject: Re: [NOVICE] aggregate function ?
>
> I see I wasn't clear enough ...
>
> All the records are from the same table, the letters were just row
> data.
>
> So I should know the value of the row that is before the one I'm
> going to fetch ...
>
>
> thanks!
>
> raimon
>
>
> On 15/05/2007, at 23:59, Richard Broersma Jr wrote:
>
>>
>> --- Raimon Fernandez <coder(at)montx(dot)com> wrote:
>>
>>> hi again,
>>>
>>>
>>> Some days ago I asked for help, nobody replied, and after trying to
>>> do it in some way, I think aggregate function is the solution to my
>>> problem, but I found difficult to understand how it works ...
>>>
>>> What I want to do is accumulate the value for each row and add it to
>>> the next:
>>>
>>> a 100 100
>>> b 50 150
>>> c 25 175
>>>
>> One solution is to use a correlated sub-query:
>>
>> SELECT A.letter_field, A.letter_value,
>> ( SELECT sum( letter_value )
>> FROM Your_table AS B
>> WHERE B.letter_field <= A.letter_field ) AS value_sum
>> FROM Your_table AS A
>> ORDER BY A.letter_field;
>>
>> Another solution is to use a join:
>>
>> SELECT A.letter_field, A.letter_value,
>> sum( B.letter_value ) AS value_sum
>> FROM Your_table AS A
>> INNER JOIN Your_table AS B
>> ON A.letter_field <= B.letter_field
>> GROUP BY A.letter_field, A.letter_value,
>> ORDER BY A.letter_field;
>>
>> There are a couple of good SQL books that are really good at
>> teaching methods on how to construct
>> such queries if you are enterested.
>>
>> Regards,
>> Richard Broersma Jr.
>>
>>
>

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message John DeSoi 2007-05-15 23:54:19 Re: Invalid byte sequence for encoding "UTF8"
Previous Message Richard Broersma Jr 2007-05-15 21:59:52 Re: aggregate function ?