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.casan...@2ndquadrant.com> escribió: > On Tue, 16 Oct 2018 at 19:30, Anthony Sotolongo <asotolo...@gmail.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 >