안녕하세요~ 요즘 날씨가 엄청더워졌네요.... 모두들 무더위 조심하시고 오늘도 즐거운 하루 되세요~
TABLE1, TABLE2, TABLE3 의 KEY_BUNHO를 기준으로 1:1 조인하여 존재 유무를 확인하려고 합니다.
TABLE1 | TABLE2 | TABLE3 | |||||
KEY_BUNHO | NAME1 | KEY_BUNHO | NAME1 | KEY_BUNHO | NAME1 | ||
15011996863006 | 사과 | 15011996863006 | 사과 | 15011996863006 | 사과 | ||
15011996863008 | 배 | 15011996863008 | 배 | 15011996863008 | 배 | ||
15012006005729 | 바나나 | 15012006005729 | 바나나 | 15012006005729 | 바나나 | ||
15012006005731 | 수박 | 15012006005731 | 수박 | 15012006005731 | 수박 | ||
15012006005732 | 포도 | 15012006005732 | 포도 | 15471996463933 | 생선 | ||
15012006005847 | 귤 | 15012006005907 | 키위 | 15471996613856 | 배추 | ||
15012006005848 | 딸기 | 15012006005910 | 감 | 15471996613861 | 시금치 |
위 KEY_BUNHO 별 존재 여부 얻고자 하는 결과
TABLE1 | TABLE2 | TABLE3 | 건수 |
O | O | O | |
O | O | X | |
O | X | X | |
O | X | O | |
X | O | O | |
X | O | X | |
X | X | O |
많은 조언 부탁 드리겠습니다~
SELECT NVL(T1.KEY_B,T2.KEY_B), T1.N1 , T1.N2 , T2.N N3 FROM (SELECT NVL(A.KEY_B,B.KEY_B) KEY_B , A.N AS N1 , B.N AS N2 FROM ( SELECT '15011996863006' KEY_B , '사과' N FROM DUAL UNION ALL SELECT '15011996863008' KEY_B , '배' N FROM DUAL UNION ALL SELECT '15012006005729' KEY_B , '바나나' N FROM DUAL UNION ALL SELECT '15012006005731' KEY_B , '수박' N FROM DUAL UNION ALL SELECT '15012006005732' KEY_B , '포도' N FROM DUAL UNION ALL SELECT '15012006005847' KEY_B , '귤' N FROM DUAL UNION ALL SELECT '15012006005848' KEY_B , '딸기' N FROM DUAL ) A FULL OUTER JOIN ( SELECT '15011996863006' KEY_B , '사과 ' N FROM DUAL UNION ALL SELECT '15011996863008' KEY_B , '배 ' N FROM DUAL UNION ALL SELECT '15012006005729' KEY_B , '바나나' N FROM DUAL UNION ALL SELECT '15012006005731' KEY_B , '수박 ' N FROM DUAL UNION ALL SELECT '15012006005732' KEY_B , '포도 ' N FROM DUAL UNION ALL SELECT '15012006005907' KEY_B , '키위 ' N FROM DUAL UNION ALL SELECT '15012006005910' KEY_B , '감 ' N FROM DUAL ) B ON A.KEY_B = B.KEY_B ) T1 FULL OUTER JOIN ( SELECT '15011996863006 ' KEY_B , '사과 ' N FROM DUAL UNION ALL SELECT '15011996863008 ' KEY_B , '배 ' N FROM DUAL UNION ALL SELECT '15012006005729 ' KEY_B , '바나나 ' N FROM DUAL UNION ALL SELECT '15012006005731 ' KEY_B , '수박 ' N FROM DUAL UNION ALL SELECT '15471996463933 ' KEY_B , '생선 ' N FROM DUAL UNION ALL SELECT '15471996613856 ' KEY_B , '배추 ' N FROM DUAL UNION ALL SELECT '15471996613861 ' KEY_B , '시금치 ' N FROM DUAL ) T2 ON T1.KEY_B = T2.KEY_B
-- FULL OUTER JOIN 이용하는 경우입니다.
-- UNION ALL 을 사용하는건 다음분이~~
WITH table1 AS ( SELECT 15011996863006 key_bunho, '사과' name1 FROM dual UNION ALL SELECT 15011996863008, '배' FROM dual UNION ALL SELECT 15012006005729, '바나나' FROM dual UNION ALL SELECT 15012006005731, '수박' FROM dual UNION ALL SELECT 15012006005732, '포도' FROM dual UNION ALL SELECT 15012006005847, '귤' FROM dual UNION ALL SELECT 15012006005848, '딸기' FROM dual ) , table2 AS ( SELECT 15011996863006 key_bunho, '사과' name1 FROM dual UNION ALL SELECT 15011996863008, '배' FROM dual UNION ALL SELECT 15012006005729, '바나나' FROM dual UNION ALL SELECT 15012006005731, '수박' FROM dual UNION ALL SELECT 15012006005732, '포도' FROM dual UNION ALL SELECT 15012006005907, '키위' FROM dual UNION ALL SELECT 15012006005910, '감' FROM dual ) , table3 AS ( SELECT 15011996863006 key_bunho, '사과' name1 FROM dual UNION ALL SELECT 15011996863008, '배' FROM dual UNION ALL SELECT 15012006005729, '바나나' FROM dual UNION ALL SELECT 15012006005731, '수박' FROM dual UNION ALL SELECT 15471996463933, '생선' FROM dual UNION ALL SELECT 15471996613856, '배추' FROM dual UNION ALL SELECT 15471996613861, '시금치' FROM dual ) SELECT ox1, ox2, ox3 , COUNT(*) cnt FROM (SELECT key_bunho , MIN(ox1) ox1 , MIN(ox2) ox2 , MIN(ox3) ox3 FROM (SELECT key_bunho, 'O' ox1, 'X' ox2, 'X' ox3 FROM table1 UNION ALL SELECT key_bunho, 'X' ox1, 'O' ox2, 'X' ox3 FROM table2 UNION ALL SELECT key_bunho, 'X' ox1, 'X' ox2, 'O' ox3 FROM table3 ) GROUP BY key_bunho ) GROUP BY ox1, ox2, ox3 ORDER BY ox1, ox2, ox3 ;