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
운영게시판
최근게시물
PostgreSQL Q&A 9962 게시물 읽기
No. 9962
[질문]세로데이터를 가로로 바꿀려고합니다.
작성자
차상환
작성일
2018-04-03 16:34:59ⓒ
2018-04-03 16:51:29ⓜ
조회수
800

WITH DT AS

(

SELECT 1 AS M_RN, 'A' AS COL1, 'a' AS COL2 FROM DUAL

UNION ALL SELECT 2 AS M_RN, 'B' AS COL1, 'b' AS COL2 FROM DUAL

UNION ALL SELECT 3 AS M_RN, 'C' AS COL1, 'c' AS COL2 FROM DUAL

UNION ALL SELECT 4 AS M_RN, 'D' AS COL1, 'd' AS COL2 FROM DUAL

UNION ALL SELECT 5 AS M_RN, 'E' AS COL1, 'e' AS COL2 FROM DUAL

UNION ALL SELECT 6 AS M_RN, 'F' AS COL1, 'f' AS COL2 FROM DUAL

UNION ALL SELECT 7 AS M_RN, 'G' AS COL1, 'g' AS COL2 FROM DUAL

UNION ALL SELECT 8 AS M_RN, 'H' AS COL1, 'h' AS COL2 FROM DUAL

UNION ALL SELECT 9 AS M_RN, 'I' AS COL1, 'i' AS COL2 FROM DUAL

UNION ALL SELECT 10 AS M_RN, 'J' AS COL1, 'j' AS COL2 FROM DUAL

UNION ALL SELECT 11 AS M_RN, 'K' AS COL1, 'k' AS COL2 FROM DUAL

UNION ALL SELECT 12 AS M_RN, 'L' AS COL1, 'l' AS COL2 FROM DUAL

)

SELECT * FROM DT

 

위 데이터를 M_RN기준으로 가로로 조회하고 싶어 문의 드립니다.

 

C1 | C2 | C3 | C4 | C5 | C6 | ... | C12

A | B | C | D | E | F | ... | L

a | b | c | d | e | f | ... | l

 

 

 

 

 

 

 

 

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

WITH dt AS
(
SELECT 1 m_rn, 'A' col1, 'a' col2
UNION ALL SELECT  2, 'B', 'b'
UNION ALL SELECT  3, 'C', 'c'
UNION ALL SELECT  4, 'D', 'd'
UNION ALL SELECT  5, 'E', 'e'
UNION ALL SELECT  6, 'F', 'f'
UNION ALL SELECT  7, 'G', 'g'
UNION ALL SELECT  8, 'H', 'h'
UNION ALL SELECT  9, 'I', 'i'
UNION ALL SELECT 10, 'J', 'j'
UNION ALL SELECT 11, 'K', 'k'
UNION ALL SELECT 12, 'L', 'l'
)
SELECT lv
     , MIN(CASE m_rn WHEN  1 THEN c END) c01
     , MIN(CASE m_rn WHEN  2 THEN c END) c02
     , MIN(CASE m_rn WHEN  3 THEN c END) c03
     , MIN(CASE m_rn WHEN  4 THEN c END) c04
     , MIN(CASE m_rn WHEN  5 THEN c END) c05
     , MIN(CASE m_rn WHEN  6 THEN c END) c06
     , MIN(CASE m_rn WHEN  7 THEN c END) c07
     , MIN(CASE m_rn WHEN  8 THEN c END) c08
     , MIN(CASE m_rn WHEN  9 THEN c END) c09
     , MIN(CASE m_rn WHEN 10 THEN c END) c10
     , MIN(CASE m_rn WHEN 11 THEN c END) c11
     , MIN(CASE m_rn WHEN 12 THEN c END) c12
  FROM (SELECT m_rn
             , lv
             , CASE lv WHEN 1 THEN col1 WHEN 2 THEN col2 END c
          FROM dt
             , (SELECT 1 lv UNION ALL SELECT 2) a
        ) a
 GROUP BY lv
 ORDER BY lv
;

마농(manon94)님이 2018-04-03 18:32:53에 작성한 댓글입니다.

감사합니다.

차상환님이 2018-04-04 08:00:43에 작성한 댓글입니다. Edit
[Top]
No.
제목
작성자
작성일
조회
9965Postgresql Management tool [1]
이성필
2018-04-17
1077
9964BLOB형식을 어떻게 사용해야하는지 알려주세요ㅠㅠ [1]
HAE
2018-04-11
1025
9963전체 function에서 내역 검색하는 방법 있나요? [1]
강정회
2018-04-09
800
9962[질문]세로데이터를 가로로 바꿀려고합니다. [2]
차상환
2018-04-03
800
9961Postgresql10을 사용하기 위한 pgadmin3 의 대체제 [2]
이성필
2018-03-28
864
9960distinct 관련 쿼리 [1]
박병호
2018-03-25
763
9959트리거에 Mysql에 값을 넣는게 가능할까요? [3]
이기자
2018-03-22
747
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2018 DSN, All rights reserved.
작업시간: 0.069초, 이곳 서비스는
	PostgreSQL v10.4로 자료를 관리합니다