Re: Advice on how to build a query with partial sums

From: David Johnston <polobo(at)yahoo(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: Advice on how to build a query with partial sums
Date: 2013-09-01 02:54:49
Message-ID: 1378004089012-5769226.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

JORGE MALDONADO wrote
> I need to build a query for the sample data below:
>
> -----------------------------------------------------------------------
> --------
> fte_clave fte_padre fte_porc fte_porc_aux total
> -----------------------------------------------------------------------
> --------
> 1 1 25 25
> 50
> 2 1 25 0
> 50
> 3 1 50 25
> 50
>
> 4 2 20 0
> 40
> 5 2 20 25
> 40
> 6 2 60 15
> 40
>
> 7 3 15 35
> 60
> 8 3 15 0
> 60
> 9 3 20 0
> 60
> 10 3 50 25 60
> -----------------------------------------------------------------------
> -------
>
> The table contains only the first 4 fields and I want to build a query
> that
> gets such fields plus a fifth one (total) with the sum of "fte_porc_aux"
> for each "fte_padre".
>
> I will very much appreciate your comments.

Look up the concept of "window functions" in the documentation.

Select sum(fte_porc_aux) over (partition by fte_padre) from ...

David J.

--
View this message in context: http://postgresql.1045698.n5.nabble.com/Advice-on-how-to-build-a-query-with-partial-sums-tp5769216p5769226.html
Sent from the PostgreSQL - novice mailing list archive at Nabble.com.

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Athanasios Kostopoulos 2013-09-02 12:49:38 extension built-in but now showing in dx/dx+
Previous Message JORGE MALDONADO 2013-08-31 17:38:39 Advice on how to build a query with partial sums