Re: Obtener columna de totales

From: Anthony Sotolongo <asotolongo(at)gmail(dot)com>
To: Jaime Casanova <jaime(dot)casanova(at)2ndquadrant(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-18 02:31:15
Message-ID: CAASDfF0adehjAGGXwHB5G4g8bc72G1o+rAeoGNtU3OXStxWKFQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

Gracias Jaime, bien simple, tanto uso del with me ha hecho dependiente de
el, jeje

Saludos

El mié., 17 de oct. de 2018 12:14 p.m., Jaime Casanova <
jaime(dot)casanova(at)2ndquadrant(dot)com> escribió:

> 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

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Eduardo Morras 2018-10-18 08:31:29 Error introduciendo datos tipo numeric
Previous Message Jaime Casanova 2018-10-17 15:14:40 Re: Obtener columna de totales