database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
PostgreSQL
Firebird
ㆍOracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
Oracle Q&A 40516 게시물 읽기
No. 40516
트리 구조 쿼리 질문
작성자
RN
작성일
2014-07-01 11:27ⓒ
2014-07-01 11:30ⓜ
조회수
7,923

SELECT '000' AS PAR, '11P' AS CHI FROM DUAL UNION ALL
SELECT '11P' AS PAR, '1EX' AS CHI FROM DUAL UNION ALL
SELECT '1EX' AS PAR, '1EA' AS CHI FROM DUAL UNION ALL
SELECT '1EA' AS PAR, '1EK' AS CHI FROM DUAL UNION ALL
SELECT '1EK' AS PAR, '1E1' AS CHI FROM DUAL UNION ALL
SELECT '000' AS PAR, '11S' AS CHI FROM DUAL UNION ALL
SELECT '11S' AS PAR, '1RY' AS CHI FROM DUAL UNION ALL
SELECT '1RY' AS PAR, '1RM' AS CHI FROM DUAL UNION ALL
SELECT '1RM' AS PAR, '1R3' AS CHI FROM DUAL UNION ALL
SELECT '000' AS PAR, '11S' AS CHI FROM DUAL UNION ALL
SELECT '11S' AS PAR, '1RY' AS CHI FROM DUAL UNION ALL
SELECT '1RY' AS PAR, '1RN' AS CHI FROM DUAL UNION ALL
SELECT '1RN' AS PAR, '1R9' AS CHI FROM DUAL UNION ALL
SELECT '000' AS PAR, '11S' AS CHI FROM DUAL UNION ALL
SELECT '11S' AS PAR, '1RY' AS CHI FROM DUAL UNION ALL
SELECT '1RY' AS PAR, '1S3' AS CHI FROM DUAL

 

위 데이터를 아래와 같이 5개의 컬럼으로 표현하고 싶은데 가능할까요?

트리구조에서 '000' 의 CHI는 COL1이 되고 그다음부터 순차적으로 COL2, COL3, COL4가 되며

가장 하위 데이터는 무조건 COL5가 되어야 합니다.

 

COL1 COL2 COL3 COL4 COL5
========================
11P  1EX  1EA  1EK  1E1
11S  1RY  1RM          1R3
11S  1RY  1RN          1R9
11S  1RY                     1S3

이 글에 대한 댓글이 총 2건 있습니다.

WITH t AS
(
SELECT '000' AS PAR, '11P' AS CHI FROM DUAL UNION ALL
SELECT '11P' AS PAR, '1EX' AS CHI FROM DUAL UNION ALL
SELECT '1EX' AS PAR, '1EA' AS CHI FROM DUAL UNION ALL
SELECT '1EA' AS PAR, '1EK' AS CHI FROM DUAL UNION ALL
SELECT '1EK' AS PAR, '1E1' AS CHI FROM DUAL UNION ALL
SELECT '000' AS PAR, '11S' AS CHI FROM DUAL UNION ALL
SELECT '11S' AS PAR, '1RY' AS CHI FROM DUAL UNION ALL
SELECT '1RY' AS PAR, '1RM' AS CHI FROM DUAL UNION ALL
SELECT '1RM' AS PAR, '1R3' AS CHI FROM DUAL UNION ALL
--SELECT '000' AS PAR, '11S' AS CHI FROM DUAL UNION ALL -- 중복
--SELECT '11S' AS PAR, '1RY' AS CHI FROM DUAL UNION ALL -- 중복
SELECT '1RY' AS PAR, '1RN' AS CHI FROM DUAL UNION ALL
SELECT '1RN' AS PAR, '1R9' AS CHI FROM DUAL UNION ALL
--SELECT '000' AS PAR, '11S' AS CHI FROM DUAL UNION ALL -- 중복
--SELECT '11S' AS PAR, '1RY' AS CHI FROM DUAL UNION ALL -- 중복
SELECT '1RY' AS PAR, '1S3' AS CHI FROM DUAL
)
-- 예시자료에 문제가 있네요???
-- 중복자료는 제거해야 맞겠죠?
SELECT REGEXP_SUBSTR(p, '[^-]+', 1, 2) c1
     , REGEXP_SUBSTR(p, '[^-]+', 1, 3) c2
     , REGEXP_SUBSTR(p, '[^-]+', 1, 4) c3
     , REGEXP_SUBSTR(p, '[^-]+', 1, 5) c4
     , chi                             c5
  FROM (SELECT SYS_CONNECT_BY_PATH(par, '-') p
             , chi
          FROM t
         WHERE CONNECT_BY_ISLEAF = 1
         START WITH par = '000'
         CONNECT BY PRIOR chi = par
        )
;

마농(manon94)님이 2014-07-01 16:44에 작성한 댓글입니다.

아..죄송합니다. 제가 중복데이터를 올렸네요..

이렇게 간단하게 되는줄 몰랐네요..

정말 큰 도움 되었습니다.

감사합니다^^

RN님이 2014-07-01 17:12에 작성한 댓글입니다. Edit
[Top]
No.
제목
작성자
작성일
조회
40519rownum과 order by를 동시에 썼을 때 성능..
corny5
2014-07-08
7206
40518[질문] 마지막 노드의 값을 가져오고 싶습니다. [1]
차상환
2014-07-07
7502
40517쿼리 문의 [1]
gome
2014-07-03
6890
40516트리 구조 쿼리 질문 [2]
RN
2014-07-01
7923
40515오라클에서 mssql db link 할때 odbc 추가하지 않고도 가능한 방법이 있나요? [1]
KMY
2014-06-30
7285
40514QTODBC 설정
이상미
2014-06-29
7358
40513결과 값을 치환하고 싶은데요 [2]
초보
2014-06-27
7510
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.019초, 이곳 서비스는
	PostgreSQL v16.4로 자료를 관리합니다