안녕하세요.
하루종일 DB사랑넷과 구루비에서 찾아봤는데 검색 단어를 몰라 결국 찾는데 실패했습니다. (ㅠ.ㅠ)
A, B 두 테이블이 있습니다. AAA, BBB, CCC(대,중,소) 라는 컬럼명이 존재하는데 A테이블을 기준으로 B 테이블 정보를 불러옵니다.
그런데 B 테이블과 A 테이블에 AAA 라는 컬럼명에 동일 데이터가 존재하면 해당 데이터만 표현하고 BBB, CCC는 NULL 로 표현합니다. 물론 A테이블은 전부 표현을 합니다.
AAA, BBB 가 존재하면 AAA, BBB를 표현합니다.
제가 열심히 삽질해서 결국 얻은 결론은 스칼라 쿼리밖에 생각이 나질 않아서 하기 처럼 작성하였습니다.
그런데 아무리 생각해도 대,중,소를 스칼라 쿼리로 가져오는 것은 아닌것 같아서요. 데이터가 한두건도 아니고...
그래서 고수님들의 도움을 받고자 합니다.
오라클 버전은 11G 입니다.
WITH BASE AS
(
SELECT 'C' AS PPPP, 'CLE' AS RRRR, 'CU' AS LLLL, 'LK' AS MMMM, 'NSM' AS SSSS FROM DUAL UNION ALL
SELECT 'C' AS PPPP, 'CLE' AS RRRR, 'SIN' AS LLLL, 'FNM' AS MMMM, 'NSM' AS SSSS FROM DUAL UNION ALL
SELECT 'C' AS PPPP, 'CLE' AS RRRR, 'SIN' AS LLLL, 'S_FNS' AS MMMM, 'CEL' AS SSSS FROM DUAL UNION ALL
SELECT 'C' AS PPPP, 'CLE' AS RRRR, 'SIN' AS LLLL, 'S_HS' AS MMMM, 'SU3' AS SSSS FROM DUAL UNION ALL
SELECT 'C' AS PPPP, 'CLE' AS RRRR, 'ASC' AS LLLL, 'ESD' AS MMMM, 'FFA' AS SSSS FROM DUAL
),
SUB AS
(
SELECT 'C' AS PPPP, 'CLE' AS RRRR, 'CU' AS LLLL, 'CU_E' AS MMMM, 'SAB' AS SSSS FROM DUAL UNION ALL
SELECT 'C' AS PPPP, 'CLE' AS RRRR, 'CU' AS LLLL, 'FOUP' AS MMMM, 'UP' AS SSSS FROM DUAL UNION ALL
SELECT 'C' AS PPPP, 'CLE' AS RRRR, 'CU' AS LLLL, 'CU_F' AS MMMM, 'NS' AS SSSS FROM DUAL UNION ALL
SELECT 'C' AS PPPP, 'CLE' AS RRRR, 'SIN' AS LLLL, 'FNM' AS MMMM, 'NSM' AS SSSS FROM DUAL UNION ALL
SELECT 'C' AS PPPP, 'CLE' AS RRRR, 'SIN' AS LLLL, 'FNM' AS MMMM, 'NSM' AS SSSS FROM DUAL UNION ALL
SELECT 'C' AS PPPP, 'CLE' AS RRRR, 'SIN' AS LLLL, 'FNM' AS MMMM, 'NSMD' AS SSSS FROM DUAL UNION ALL
SELECT 'C' AS PPPP, 'CLE' AS RRRR, 'SIN' AS LLLL, 'S_HS' AS MMMM, 'NSD' AS SSSS FROM DUAL UNION ALL
SELECT 'C' AS PPPP, 'CLE' AS RRRR, 'SIN' AS LLLL, 'S_HS' AS MMMM, 'NSE' AS SSSS FROM DUAL UNION ALL
SELECT 'C' AS PPPP, 'CLE' AS RRRR, 'SIN' AS LLLL, 'S_HS' AS MMMM, 'NSG' AS SSSS FROM DUAL UNION ALL
SELECT 'C' AS PPPP, 'CLE' AS RRRR, 'DES' AS LLLL, 'S_HS' AS MMMM, 'NSG' AS SSSS FROM DUAL
)
SELECT A.PPPP, A.RRRR, A.LLLL, A.MMMM, A.SSSS
,(SELECT MAX(LLLL) FROM SUB WHERE A.PPPP = PPPP AND A.RRRR = RRRR AND A.LLLL = LLLL ) AS B_LLLL
,(SELECT MAX(MMMM) FROM SUB WHERE A.PPPP = PPPP AND A.RRRR = RRRR AND A.LLLL = LLLL AND A.MMMM = MMMM) AS B_LLLL
,(SELECT MAX(SSSS) FROM SUB WHERE A.PPPP = PPPP AND A.RRRR = RRRR AND A.LLLL = LLLL AND A.MMMM = MMMM AND A.SSSS = SSSS) AS B_LLLL
FROM BASE A
ORDER BY A.PPPP, A.RRRR, A.LLLL, A.MMMM, A.SSSS
감사합니다. |