Oracle SQL - 将两列合并为一列

Oracle SQL - Combine two columns into one

我有一个table

Column 1       Column 2
   A              B
   B              C
   C              D
   C              E

现在我想要如下输出(从 A 到终点的所有可能路线,如 A-B-C-D、A-B-C-E)

Column 1 
   A
   B
   C
   D
   A
   B
   C
   E

您需要记录通过您的节点的路径,并且只有 return 完整路径,因此以下内容应该可以帮助您:

with dat as (
select 'A' col1, 'B' col2 from dual union all
select 'B' col1, 'C' col2 from dual union all
select 'C' col1, 'D' col2 from dual union all
select 'C' col1, 'E' col2 from dual )
select ltrim(the_path,'-')||'-'||col2
from (
    select SYS_CONNECT_BY_PATH(col1, '-') the_path
          ,CONNECT_BY_ISLEAF is_leaf
          ,col2
    from dat
    start with col1 = 'A'
    connect by prior col2 = col1
    ) where is_leaf = 1;

您想要这样的东西吗?

with sample_data as (select 'A' col1, 'B' col2 from dual union all
                     select 'B' col1, 'C' col2 from dual union all
                     select 'C' col1, 'D' col2 from dual union all
                     select 'C' col1, 'E' col2 from dual union all
                     select 'A' col1, 'F' col2 from dual)
select connect_by_root(col1)||sys_connect_by_path(col2, '-') route
from   sample_data
where  connect_by_isleaf = 1
connect by prior col2 = col1
start with col1 = 'A';

ROUTE
---------
A-B-C-D
A-B-C-E
A-F