SELECT D1.SUB AS SUB1 , D1.SUB_NM AS SUB_NM1 , D1.CDT AS CDT1 , D2.SUB AS SUB2 , D2.SUB_NM AS SUB_NM2 , D2.CDT AS CDT2 FROM ( SELECT 'A001' AS SUB, '교양1' AS SUB_NM, 1 AS CDT FROM DUAL UNION SELECT 'A002' AS SUB, '교양2' AS SUB_NM, 1 AS CDT FROM DUAL ) D1 FULL OUTER JOIN ( SELECT 'A002' AS SUB, '교양2' AS SUB_NM, 1 AS CDT FROM DUAL UNION SELECT 'B001' AS SUB, '전공1' AS SUB_NM, 2 AS CDT FROM DUAL UNION SELECT 'B002' AS SUB, '전공2' AS SUB_NM, 3 AS CDT FROM DUAL ) D2 ON D1.SUB = D2.SUB
SUB1 | SUB_NM1 | CDT1 | SUB2 | SUB_NM2 | CDT2 |
A002 | 교양2 | 1 | A002 | 교양2 | 1 |
B002 | 전공2 | 3 | |||
B001 | 전공1 | 2 | |||
A001 | 교양1 | 1 |
이런 데이터 구조라고 봤을 때
중복된 것은 그대로 보여지고 겹치지 않는 값들은 공백으로 하나씩 나오는게 아니라
아래처럼 그냥 순서대로 쭉 나열해주고 싶습니다..
SUB1 | SUB_NM1 | CDT1 | SUB2 | SUB_NM2 | CDT2 |
A002 | 교양2 | 1 | A002 | 교양2 | 1 |
A001 | 교양1 | 1 | B001 | 전공1 | 2 |
B002 | 전공2 | 3 |
보통 조인이란 데이터간 상관이 relation이 있어야 되는데 그럴 필요가 없는 요구네요.
with D1_s as
( SELECT 'A001' AS SUB, '교양1' AS SUB_NM, 1 AS CDT FROM DUAL
UNION
SELECT 'A002' AS SUB, '교양2' AS SUB_NM, 1 AS CDT FROM DUAL
)
,D2_s as
(SELECT 'A002' AS SUB, '교양2' AS SUB_NM, 1 AS CDT FROM DUAL
UNION
SELECT 'B001' AS SUB, '전공1' AS SUB_NM, 2 AS CDT FROM DUAL
UNION
SELECT 'B002' AS SUB, '전공2' AS SUB_NM, 3 AS CDT FROM DUAL
)
, d1 as (select d1_s.*, rownum rn from d1_s )
, d2 as (select d2_s.*, rownum rn from d2_s )
SELECT D1.SUB AS SUB1
, D1.SUB_NM AS SUB_NM1
, D1.CDT AS CDT1
, D2.SUB AS SUB2
, D2.SUB_NM AS SUB_NM2
, D2.CDT AS CDT2
FROM d1
FULL OUTER JOIN
d2
ON D1.rn = D2.rn
WITH d1 AS ( SELECT 'A001' sub, '교양1' sub_nm, 1 cdt FROM dual UNION ALL SELECT 'A002', '교양2', 1 FROM dual ) , d2 AS ( SELECT 'A002' sub, '교양2' sub_nm, 1 cdt FROM dual UNION ALL SELECT 'B001', '전공1', 2 FROM dual UNION ALL SELECT 'B002', '전공2', 3 FROM dual ) SELECT MIN(sub1) sub1 , MIN(sub_nm1) sub_nm1 , MIN(cdt1) cdt1 , MIN(sub2) sub2 , MIN(sub_nm2) sub_nm2 , MIN(cdt2) cdt2 FROM (SELECT d1.sub sub1, d1.sub_nm sub_nm1, d1.cdt cdt1 , d2.sub sub2, d2.sub_nm sub_nm2, d2.cdt cdt2 , DECODE(d1.sub, d2.sub, 1, 2) gb1 , DENSE_RANK() OVER( PARTITION BY DECODE(d1.sub, d2.sub, 1, null, 2, 3) ORDER BY NVL(d1.sub, d2.sub) ) gb2 FROM d1 FULL OUTER JOIN d2 ON d1.sub = d2.sub ) GROUP BY gb1, gb2 ORDER BY gb1, gb2 ;