Поскольку требования расплывчаты, я не могу с уверенностью сказать, что следующее решение завершено, однако я хотел бы разбить p0, p1, p2 на подзапросы, чтобы вы могли использовать p0 в p1 и p2. Например:
with cars as
(
select 2 id_car, 'Ford' car, 'Ok' state_car from dual union
select 1 id_car, 'Audi' car, 'Ok' state_car from dual
)
, garages as
(
select 2 id_car, 'P02' garage from dual union
select 1 id_car, 'P01' garage from dual
)
, v_collected as
(
select car.id_car
,car.car
,car.state_car
,garage.garage
from cars car
,garages garage
where 1=1
and car.id_car = garage.id_car
)
-- select * from v_collected;
, p0_subquery as
(
select 0 as score
,p0.*
from v_collected p0
where 1 = 1
-- User IO Binding
and p0.car = 'Audi'
and p0.garage = 'P01'
and p0.state_car = 'Ok'
)
--select * from p0_subquery;
, p1_subquery as
(
select 1 as score
,p1.*
from v_collected p1
, p0_subquery p0
where 1 = 1
-- Should access the identical binding
and p1.car <> p0.car
and p1.state_car = p0.state_car
)
, p2_subquery as
(
select 2 as score
,p2.*
from v_collected p2
, p0_subquery p0
where 1 = 1
-- Should access the identical binding
and p2.state_car = p0.state_car
)
select * from p0_subquery
union
select * from p1_subquery
union
select * from p2_subquery
;