How to get a similar value in Oracle

前端 未结 2 1496
春和景丽
春和景丽 2021-01-28 09:00

I have a table of two columns

Col1  Col2
A        1
A        2
A        3
B        1
B        2
B        3

Output I need is like this



        
2条回答
  •  悲&欢浪女
    2021-01-28 09:14

    with s (Col1, Col2) as (
    select 'A', 1 from dual union all
    select 'A', 2 from dual union all
    select 'A', 3 from dual union all
    select 'B', 1 from dual union all
    select 'B', 2 from dual union all
    select 'B', 3 from dual)
    select col1, ltrim(sys_connect_by_path(col2, ','), ',') path
    from s
    start with col2 = 1
    connect by prior col2 = col2 - 1 and prior col1 = col1;
    
    C PATH
    - ----------
    A 1
    A 1,2
    A 1,2,3
    B 1
    B 1,2
    B 1,2,3
    
    6 rows selected.
    

提交回复
热议问题