Re: Obtener columna de totales

From: Jaime Casanova <jaime(dot)casanova(at)2ndquadrant(dot)com>
To: Anthony Sotolongo <asotolongo(at)gmail(dot)com>
Cc: Jorge Barzola <jorgebarzola(at)gmail(dot)com>, POSTGRES <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: Re: Obtener columna de totales
Date: 2018-10-17 15:14:40
Message-ID: CAJGNTeOutcLpEyOaXypucBJgSpD8C19jDgpbu8CQz0HGxAejTg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

On Tue, 16 Oct 2018 at 19:30, Anthony Sotolongo <asotolongo(at)gmail(dot)com> wrote:
>
> Hola Jorge, puedes utilizar las funciones ventanas(https://www.postgresql.org/docs/10/static/functions-window.html), algo como esto, debes ajustarlo a tu escenario:
>
> with a as (
> select row_number() over ()as id, id_tip_mov,mto_can, case id_tip_mov
> when 2 then 0-mto_can
> else
> mto_can
> end as valor
> from (select * from (VALUES (1, 100), (1, 300),(2, 6),(2, 4),(1,7)) AS t (id_tip_mov,mto_can)) sub)
>
> select id_tip_mov,mto_can,sum (valor) over (order by id rows between unbounded preceding and current row) from a
>

me parece que te estas complicando de mas, el WITH no lo necesitas.
debería bastar con:

select fec_emi, id_tip_mov, mto_can,
sum(case when id_tip_mov = 1 then mto_can else mto_can*(-1) end)
over (order by fec_emi rows between unbounded
preceding and current row) mto_tot
from mov;

--
Jaime Casanova www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

In response to

Responses

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Anthony Sotolongo 2018-10-18 02:31:15 Re: Obtener columna de totales
Previous Message Jorge Barzola 2018-10-17 04:13:02 Re: Obtener columna de totales