i have table has 2 columns unit, m_unit shown below:
+------+--------+ | unit | m_unit | +------+--------+ | 10 | 12 | | 15 | 19 | | 12 | 16 | | 13 | 15 | | 19 | 14 | | 14 | 11 | +------+--------+
i want create column h_tree stores hierarchy comma separated values after recursively searching through unit & m_unit such h_tree starts unit , ends last possible m_unit shown below:
+------+--------+----------------+ | unit | m_unit | h_tree | +------+--------+----------------+ | 10 | 12 | 10,12,16 | | 15 | 19 | 15,19,14,11 | | 12 | 16 | 12,16 | | 13 | 15 | 13,15,19,14,11 | | 19 | 14 | 19,14,11 | | 14 | 11 | 14,11 | +------+--------+----------------+
apologies if not clear enough, let me know if confusing. thanks.
this should produce want:
with data (unit, m_unit) ( select 10, 12 dual union select 15, 19 dual union select 12, 16 dual union select 13, 15 dual union select 19, 14 dual union select 14, 11 dual) select unit, m_unit, unit || ',' || listagg(root_unit, ',') within group (order depth) h_tree ( select id, unit, m_unit, level depth, connect_by_root m_unit root_unit (select rownum id, unit, m_unit data) data connect prior unit = m_unit) group id, unit, m_unit
if lines in table distinct id
column not necessary.
Comments
Post a Comment