create or replace function f_get_rqa_forRelLoanBill_plpd(plpdId number, deliveryDate date)
  return number is
  res            number;
  v_plpdId       number := plpdId;
  v_deliveryDate date := deliveryDate;
begin
  select /*+ index (DV XPKDELIVERYVARIATIONS)*/
   nvl(sum(RQA.QUANTITY), 0) into res
    from RENTALQTYADJUSTMENT RQA
    left outer join DELIVERYVARIATIONS DV
      on RQA.DELIVERYVARIATIONS_ID = DV.DELIVERYVARIATIONS_ID
   where RQA.PRODUCTLISTPERDISTRIBPOINT_ID = v_plpdId 
   and (decode(nvl(RQA.DELIVERYVARIATIONS_ID, 0), 0, RQA.ADJUSTMENTDATE, DV.DELIVERYDATE) <= v_deliveryDate);
  return res;
end f_get_rqa_forRelLoanBill_plpd;
/
