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


saludos


El 16/10/18 a las 20:54, Jorge Barzola escribió:
Buenas noches compañeros, necesito obtener una columna de totales (MTO_TOT) como se muestra en la siguiente figura:

FEC_EMI   | ID_TIP_MOV |MTO_CAN | MTO_TOT
30/05/2018 |1         | 100   | 100
30/05/2018 |1         | 300   | 400
31/05/2018 |2         | 6         | 394
31/05/2018 |2         | 4         | 390
31/05/2018 |1         | 7         | 397

Teniendo en cuenta que:

1 = Ingreso (Suma)
2 = Salida   (Resta)

Alguna idea?

Gracias por su tiempo

Reply via email to