like this:create or replace trigger trg_demo 
before insert or update on tbname for each row
begin
if inserting or updating
  select min(b.b1) into :new.a2 where a.a1=b.id; 
end if;
end;
/