CREATE PROCEDURE "DBA"."pr_anal_arveld1"()
BEGIN
  delete from gl_pr_koond;
  -- vahekokkuvotted partnr
  insert into gl_pr_koond( konto,partnr,adeeb,akree,deeb,kree)
    select '3',gl_dk_koond.partnr,sum(gl_dk_koond.adeeb),sum(gl_dk_koond.akree),sum(gl_dk_koond.deeb),sum(gl_dk_koond.kree)
      from gl_dk_koond
      group by gl_dk_koond.partnr;
  -- loppjaagid
  update gl_pr_koond set adeeb=adeeb-akree,akree=akree-adeeb,deeb=deeb-kree,kree=kree-deeb;
  update gl_pr_koond set adeeb = 0 where adeeb < 0;
  update gl_pr_koond set akree = 0 where akree < 0;
  update gl_pr_koond set deeb = 0 where deeb < 0;
  update gl_pr_koond set kree = 0 where kree < 0;
  update gl_pr_koond set ldeeb=adeeb-akree-deeb+kree,lkree=akree-adeeb-kree+deeb;
  update gl_pr_koond set ldeeb = 0 where ldeeb < 0;
  update gl_pr_koond set lkree = 0 where lkree < 0;
END