Уверен, что вы можете сделать это с помощью иерархического запроса . Будьте осторожны - эти вещи могут уйти от вас, если вы не будете осторожны, а затем вам позвонит администратор базы данных «почему запрос выполняется в течение 6 дней». Пример:
with base_data as
(
select 1 as tax_id, 'A' as account_number from dual union all
select 1 as tax_id, 'B' as account_number from dual union all
select 2 as tax_id, 'A' as account_number from dual union all
select 2 as tax_id, 'B' as account_number from dual union all
select 2 as tax_id, 'C' as account_number from dual union all
select 3 as tax_id, 'D' as account_number from dual union all
select 3 as tax_id, 'E' as account_number from dual union all
select 4 as tax_id, 'E' as account_number from dual union all
select 4 as tax_id, 'F' as account_number from dual union all
select 5 as tax_id, 'A' as account_number from dual union all
select 5 as tax_id, 'F' as account_number from dual union all
select 6 as tax_id, 'G' as account_number from dual union all
select 7 as tax_id, 'H' as account_number from dual union all
select 7 as tax_id, 'I' as account_number from dual union all
select 8 as tax_id, 'I' as account_number from dual union all
select 8 as tax_id, 'K' as account_number from dual
)
select distinct bd.*
from base_data bd
start with tax_id = :taxID
connect by nocycle (prior account_number = account_number or prior tax_id = tax_id)
--and level <= 10
order by 1, 2
Как написано, с "и уровнем <= 10" закомментировано, это вернет все элементы. Может захотеть раскомментировать его и установить максимальный уровень - по сути, он устанавливает ограничение на то, как далеко вниз от "дыры" будет выглядеть запрос. </p>