반응형
1) SYS_CONNECT_BY_PATH
2) WM_CONCAT
with
test as
(
select '001' as id, 'Angelo' as name, 'A' as segment from dual union all
select '001' as id, 'Angelo' as name, 'D' as segment from dual union all
select '001' as id, 'Angelo' as name, 'G' as segment from dual union all
select '002' as id, 'John' as name, 'A' as segment from dual union all
select '002' as id, 'John' as name, 'B' as segment from dual union all
select '002' as id, 'John' as name, 'K' as segment from dual union all
select '002' as id, 'John' as name, 'P' as segment from dual union all
select '003' as id, 'Chloe' as name, 'Q' as segment from dual union all
select '003' as id, 'Chloe' as name, 'S' as segment from dual union all
select '004' as id, 'Dinah' as name, 'Z' as segment from dual
)
select id, name,
max(ltrim(sys_connect_by_path(segment,','),',')) keep
(dense_rank last order by level) as segment_path
from (
select test.*, row_number() over(partition by id order by segment) as rn
from test
)
start with rn = 1
connect by prior id = id
and prior rn = rn - 1
group by id, name;
2) WM_CONCAT
with
test as
(
select '001' as id, 'Angelo' as name, 'A' as segment from dual union all
select '001' as id, 'Angelo' as name, 'D' as segment from dual union all
select '001' as id, 'Angelo' as name, 'G' as segment from dual union all
select '002' as id, 'John' as name, 'A' as segment from dual union all
select '002' as id, 'John' as name, 'B' as segment from dual union all
select '002' as id, 'John' as name, 'K' as segment from dual union all
select '002' as id, 'John' as name, 'P' as segment from dual union all
select '003' as id, 'Chloe' as name, 'Q' as segment from dual union all
select '003' as id, 'Chloe' as name, 'S' as segment from dual union all
select '004' as id, 'Dinah' as name, 'Z' as segment from dual
)
select id, name, wm_concat(segment,',') segment_path
from test
group by id, name;
반응형
'프로그래밍 > Oracle' 카테고리의 다른 글
[TABLE]1.2 FILESTORAGE (0) | 2012.10.24 |
---|---|
[TABLE]1.1 SENDMAIL (0) | 2012.10.24 |
Oracle 날짜 출력 (0) | 2012.10.15 |
SELECT INSERT 예제 (0) | 2012.09.25 |
ORA-00054: 리소스가 사용 중이어서 NOWAIT가 지정되었거나 시간 초과가 만료된 상태로 획득합니다. (0) | 2012.09.04 |