create or replace function f_amountdelivered(customerid in number, productid in number, sizedefinitionid in number, finishingmethodid in number, reportparamid in number) return number is
  amountdelivered number;
begin
  select /*+ index(dh XIE16DELIVERYHISTORY)*/
        sum(dh.amountdelivered) into amountdelivered
         from   deliveryhistory dh, reportparam rp
         where  dh.customer_link_id = customerid and
                dh.product_link_id = productid and
                dh.sizedefinition_link_id = sizedefinitionid and
                dh.finishingmethod_id = finishingmethodid and
                rp.reportparam_id = reportparamid and
                dh.deliverydate between rp.date_value1 and rp.date_value2;
  return(amountdelivered);
end f_amountdelivered;
/