I need help/advice on optimizing my function. How can I avoid duplicating this fragment of code:
select 1 from order_attr oa
where oa.order_id = o.order_id and ea.order_attr_name = 'administrationId'
My PL/SQL function below (this is an example, in real code the fragment is repeated many times):
begin
select count (distinct o.order_id)
into :result
from orders o
where o.ext_order_id = :ExtOrderId and o.service_id not like 'tech#%' and (:Include = 1 and exists
(
select 1 from order_attr oa
where oa.order_id = o.order_id and ea.order_attr_name = 'administrationId'
)
or :Include = 0 and not exists
(
select 1 from order_attr oa
where oa.order_id = o.order_id and ea.order_attr_name = 'administrationId'
));
exception
when others then
:result := 0;
end;