본문 바로가기
DataBase/oracle

SQL(ORACLE) 여러 행의 특정 컬럼값 -> 하나의 컬럼으로 합치기

by 아이티.파머 2010. 2. 3.
반응형
connect by prior / sys_connect_by_path

with t as (
    select 1 key1, '가' key2, '나' key3, 10 key4 from dual union all
    select 2 key1, '가' key2, '나' key3, 20 key4 from dual union all
    select 3 key1, '다' key2, '라' key3, 30 key4 from dual union all
    select 4 key1, '다' key2, '라' key3, 40 key4 from dual union all
    select 5 key1, '다' key2, '라' key3, 50 key4 from dual union all
    select 6 key1, '마' key2, '바' key3, 60 key4 from dual union all
    select 7 key1, '마' key2, '바' key3, 70 key4 from dual union all
    select 8 key1, '가' key2, '다' key3, 10 key4 from dual
)
select key2
     , key3
     , substr(max(sys_connect_by_path(key1||':'||key4, ',')), 2)
  from (select key1
             , key2
             , key3
             , key4
             , row_number() over(partition by key2, key3 order by key4) rn
          from t
        )
 start with rn = 1
 connect by prior key2 = key2 and prior rn = rn-1
 group by key2
        , key3



반응형

'DataBase > oracle' 카테고리의 다른 글

SELECT 로 Query문 만들기  (0) 2010.04.14
ORACLE 월별 통계 (1~12월 표현)  (2) 2010.03.15
일별 통계 작성법(ORACLE)  (0) 2010.03.13
Oracle MERGE INTO(1/2)  (0) 2010.02.07
Oracle MERGE INTO(1/1)  (0) 2010.02.07