create or replace function avgPP(
  mid in member.mid%type,
  sym in security.symbol%type) 
       return transaction.price_per_share%type as 

  
  cursor c1 is 
    select trans_type, quantity, price_per_share
    from transaction
    where mid = avgPP.mid and
          symbol = sym
    order by trans_date;

  q transaction.quantity%type := 0.0;
  a transaction.price_per_share%type := 0.0;

begin
  for c1_rec in c1 loop
    if (c1_rec.trans_type = 'buy') then
      a := ((q * a) + (c1_rec.quantity * c1_rec.price_per_share))/
            (q + c1_rec.quantity);
      q := q + c1_rec.quantity;
    else
      q := q - c1_rec.quantity;
    end if;
  end loop;
  return (1.01 * a);  -- Commission of 1%
end;
/
show errors
