Удар должен сделать трюк:
with cpair (PAIR_ID, CUST_ID, MATCH_CUST_ID)
as
(select 12345, 800001, 900001 from dual
union all
select 12346, 800002, 900001 from dual
union all
select 12347, 800003, 900001 from dual
union all
select 12348, 800004, 900002 from dual
union all
select 12349, 900003, 900004 from dual
union all
select 12350, 900004, 900005 from dual
union all
select 12351, 900004, 900006 from dual) ,
tree (mainid, custid, matchcustid, path) as
(select cust_id, cust_id, match_cust_id, to_char(pair_id)
from cpair
union all
select mainid,
case when (p.CUST_ID = t.matchcustid) then p.CUST_ID
when (p.MATCH_CUST_ID = t.matchcustid) then p.match_CUST_ID
when ( p.cust_id = t.custid) then p.CUST_ID
when (p.match_cust_id = t.custid) then p.match_CUST_ID
end,
case when (p.CUST_ID = t.matchcustid) then p.MATCH_CUST_ID
when (p.MATCH_CUST_ID = t.matchcustid) then p.CUST_ID
when ( p.cust_id = t.custid) then p.MATCH_CUST_ID
when (p.match_cust_id = t.custid) then p.CUST_ID
end,
t.path || '-' || to_char(p.PAIR_ID)
from tree t
join cpair p
on
(p.CUST_ID = t.matchcustid
or p.MATCH_CUST_ID = t.matchcustid
or p.cust_id = t.custid
or p.match_cust_id = t.custid
)
and instr(path, pair_id) < 1
),
distinctTree as
(
select distinct mainid, matchcustid
from tree
union all
select distinct mainid, mainid
from tree
order by mainid),
treeGroup as (
select mainid, listagg(matchcustid, ',') within group (order by matchcustid) custList
from distinctTree
group by mainid)
select distinct custlist from treeGroup
Результат:
CustList
-------------
800004,900002
900003,900004,900005,900006
800001,800002,800003,900001
Запрос идет рекурсивно через дерево отношений с клиентами и создает список связанных клиентов для каждого клиента. После этого список связанного клиента изменяется на строку, которая используется для группировки. От группировки мы можем видеть, что клиенты принадлежат к какой группе.
Я бы сказал, что не будет простого способа, если один и тот же идентификатор может встречаться в 'CUST_ID' и в' MATCH_CUST_ID' в разных строках. –
Укажите точное условие, которое необходимо выполнить, чтобы рассмотреть два идентификатора ***, связанных ***. –
Его случай выведения ссылки. Если A связано с B и B связано с C, поэтому A также связан с C. – Reggie1977