ALTER FUNCTION "DBA"."get_makshind"(in a_maksnr integer,in a_kuup date,in a_yksusnr integer, in a_nr integer default 1)
returns numeric(15,6)
begin
  declare ldc_makshind numeric(15,6);
  select first ls_makshind.makshind into ldc_makshind from ls_makshind where ls_makshind.nr=a_nr and ls_makshind.maksnr=a_maksnr and ls_makshind.yksusnr = a_yksusnr and ls_makshind.kuup1 <= a_kuup order by ls_makshind.kuup1 desc;
  if ldc_makshind is null or ldc_makshind = 0 then
    select first ls_makshind.makshind into ldc_makshind from ls_makshind where ls_makshind.nr=a_nr and ls_makshind.maksnr=a_maksnr and ls_makshind.yksusnr is null and ls_makshind.kuup1 <= a_kuup order by ls_makshind.kuup1 desc
  end if;
  if ldc_makshind is null then
    set ldc_makshind=0
  end if;
  return ldc_makshind
end