Если вдруг кому интересно - решение я нашел еще пару лет назад. Не шибко производительное (мягко говоря), с рекурсией, но зато на SQL. Требуется Oracle 11.2 или выше.
Код:
with t as (select 'A' a, 1 b from dual union all
select 'B' a, 1 b from dual union all
select 'B' a, 2 b from dual union all
select 'C' a, 2 b from dual union all
select 'C' a, 3 b from dual union all
select 'D' a, 4 b from dual union all
select 'D' a, 5 b from dual union all
select 'E' a, 5 b from dual union all
select 'E' a, 6 b from dual),
s (a, b, root_a) as (
select a, b, connect_by_root a root_a
from t
connect by nocycle (prior a = a and prior b <> b)
or (prior a <> a and prior b = b))
select root_a, listagg(a, ',') within group (order by a) a_list
from (select distinct root_a, a
from s)
group by root_a;
Результат:
Код:
ColumnA Group
A A,B,C
B A,B,C
C A,B,C
D D,E
E D,E