Skip to content

Instantly share code, notes, and snippets.

@Dviejopomata
Created October 29, 2016 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dviejopomata/9592795f160a5ae994c3cad52967cd51 to your computer and use it in GitHub Desktop.
Save Dviejopomata/9592795f160a5ae994c3cad52967cd51 to your computer and use it in GitHub Desktop.
upsert en postgres
INSERT INTO app.messervicio as acum(id, NUMEROCOBROS, IMPORTECOBROS, ant, cargo, abono, actual, aaaa, mm, idservicios, idfactuaaaamm)
select idfactuaaaamm || '-' || idservicios id,
count(*) NUMEROCOBROS,
sum(monto) IMPORTECOBROS,
0,0,0,0,
aaaa,mm,
idservicios,
idfactuaaaamm
from app.cobros a
where a.idfactuaaaamm = '201301'
group by
idfactuaaaamm || '-' || idservicios,
aaaa,mm,
idservicios,
idfactuaaaamm
ON CONFLICT (id)
do UPDATE SET NUMEROCOBROS = acum.NUMEROCOBROS+ EXCLUDED.NUMEROCOBROS, importecobros = acum.importecobros + EXCLUDED.IMPORTECOBROS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment