안녕하세요
WITH T1 AS (
SELECT '가' AS STR ,'한글' AS STR_CD FROM DUAL UNION ALL
SELECT '나' AS STR ,'한글' AS STR_CD FROM DUAL UNION ALL
SELECT '다' AS STR ,'한글' AS STR_CD FROM DUAL UNION ALL
SELECT '라' AS STR ,'한글' AS STR_CD FROM DUAL UNION ALL
SELECT 'A' AS STR ,'영어' AS STR_CD FROM DUAL UNION ALL
SELECT 'B' AS STR ,'영어' AS STR_CD FROM DUAL UNION ALL
SELECT 'C' AS STR ,'영어' AS STR_CD FROM DUAL UNION ALL
SELECT 'D' AS STR ,'영어' AS STR_CD FROM DUAL UNION ALL
SELECT 'E' AS STR ,'영어' AS STR_CD FROM DUAL UNION ALL
SELECT 'F' AS STR ,'영어' AS STR_CD FROM DUAL
)
SELECT *
FROM T1
STR STR_CD
가 한글
나 한글
다 한글
라 한글
A 영어
B 영어
C 영어
D 영어
E 영어
F 영어
이렇게되어있는걸
STR1 STR_CD1 STR2 STR_CD2
가 한글 A 영어
나 한글 B 영어
다 한글 C 영어
라 한글 D 영어
E 영어
F 영어
이렇게 표현하고싶은데, 방법이있을까요
한글과 영어 데이터 양이 서로 많거나 서로 적을수 있습니다.
EX) 한글10개 영어 5개
한글 5개 영어 10개
각 ROWNUM을 만들고 CROSS조인하면 될것같긴한데
너무 지저분해질 것 같아서 질문드립니다.
WITH t1 AS ( SELECT '가' str, '한글' str_cd FROM dual UNION ALL SELECT '나', '한글' FROM dual UNION ALL SELECT '다', '한글' FROM dual UNION ALL SELECT '라', '한글' FROM dual UNION ALL SELECT 'A' , '영어' FROM dual UNION ALL SELECT 'B' , '영어' FROM dual UNION ALL SELECT 'C' , '영어' FROM dual UNION ALL SELECT 'D' , '영어' FROM dual UNION ALL SELECT 'E' , '영어' FROM dual UNION ALL SELECT 'F' , '영어' FROM dual ) SELECT * FROM (SELECT str, str_cd , ROW_NUMBER() OVER(PARTITION BY str_cd ORDER BY str) rn FROM t1 ) PIVOT (MIN(str) FOR str_cd IN ('한글' 한글, '영어' 영어)) ORDER BY rn ;
SELECT rn , MIN(DECODE(str_cd, '한글', str)) 한글 , MIN(DECODE(str_cd, '영어', str)) 영어 FROM (SELECT str, str_cd , ROW_NUMBER() OVER(PARTITION BY str_cd ORDER BY str) rn FROM t1 ) GROUP BY rn ORDER BY rn ;