본문 바로가기

DB/오라클

컬럼값에 따라 다른 테이블을 조인

말이조인이지 그냥 다른테이블을 디코더함수를써서 if~else처럼 조건에따라 가져오면됨

select   apmt_record_seq        -- 발령이력일련번호
        ,emp_code               -- 사원코드
        ,apmt_num               -- 발령호수번호
        ,start_date             -- 발령시작일자
        ,end_date               -- 발령종료일자
        ,title                  -- 제목
        ,disc                   -- 발령구분
        ,apmt_hist              -- 발령내역
        ,present_info           -- 발령전정보
        ,after_info             -- 발령후정보
        ,rmrk                   -- 비고
        ,( select detail_code_name from detail_code where code_disc_num='79' and detail_code = r.apmt_hist ) as apmt_hist_name      -- 발령내역 명
        ,( select detail_code_name from detail_code where code_disc_num='77' and detail_code = r.disc      ) as disc_name           -- 발령구분 명
    , DECODE( apmt_hist
                    , 'B01', ( SELECT DETAIL_CODE_NAME FROM DETAIL_CODE  WHERE code_disc_num = '5' and detail_code = r.present_info)
                    , 'B02', ( SELECT DETAIL_CODE_NAME FROM DETAIL_CODE  WHERE code_disc_num = '205' and detail_code = r.present_info)
                    , 'B04', ( SELECT DETAIL_CODE_NAME FROM DETAIL_CODE  WHERE code_disc_num = '78' and detail_code = r.present_info)
                    , 'B05', ( SELECT DETAIL_CODE_NAME FROM DETAIL_CODE  WHERE code_disc_num = '200' and detail_code = r.present_info)
                    , 'B06', ( SELECT DETAIL_CODE_NAME FROM DETAIL_CODE  WHERE code_disc_num = '143' and detail_code = r.present_info)
                    , 'B07', ( SELECT DETAIL_CODE_NAME FROM DETAIL_CODE  WHERE code_disc_num = '201' and detail_code = r.present_info)
                    , 'B08', ( SELECT DETAIL_CODE_NAME FROM DETAIL_CODE  WHERE code_disc_num = '202' and detail_code = r.present_info)
                    , 'B09', ( SELECT DETAIL_CODE_NAME FROM DETAIL_CODE  WHERE code_disc_num = '203' and detail_code = r.present_info)
                    , 'B10', ( SELECT DETAIL_CODE_NAME FROM DETAIL_CODE  WHERE code_disc_num = '152' and detail_code = r.present_info)
                    , 'B11', ( SELECT DETAIL_CODE_NAME FROM DETAIL_CODE  WHERE code_disc_num = '204' and detail_code = r.present_info)          
            ) AS PRESENT_INFO_NAME      -- 발령전정보 명
   , DECODE( apmt_hist
                    , 'B01', ( SELECT DETAIL_CODE_NAME FROM DETAIL_CODE  WHERE code_disc_num = '5' and detail_code = r.after_info)
                    , 'B02', ( SELECT DETAIL_CODE_NAME FROM DETAIL_CODE  WHERE code_disc_num = '205' and detail_code = r.after_info)
                    , 'B04', ( SELECT DETAIL_CODE_NAME FROM DETAIL_CODE  WHERE code_disc_num = '78' and detail_code = r.after_info)
                    , 'B05', ( SELECT DETAIL_CODE_NAME FROM DETAIL_CODE  WHERE code_disc_num = '200' and detail_code = r.after_info)
                    , 'B06', ( SELECT DETAIL_CODE_NAME FROM DETAIL_CODE  WHERE code_disc_num = '143' and detail_code = r.after_info)
                    , 'B07', ( SELECT DETAIL_CODE_NAME FROM DETAIL_CODE  WHERE code_disc_num = '201' and detail_code = r.after_info)
                    , 'B08', ( SELECT DETAIL_CODE_NAME FROM DETAIL_CODE  WHERE code_disc_num = '202' and detail_code = r.after_info)
                    , 'B09', ( SELECT DETAIL_CODE_NAME FROM DETAIL_CODE  WHERE code_disc_num = '203' and detail_code = r.after_info)
                    , 'B10', ( SELECT DETAIL_CODE_NAME FROM DETAIL_CODE  WHERE code_disc_num = '152' and detail_code = r.after_info)
                    , 'B11', ( SELECT DETAIL_CODE_NAME FROM DETAIL_CODE  WHERE code_disc_num = '204' and detail_code = r.after_info)          
            ) AS after_info_name        -- 발령후정보 명
from apmt_record r
WHERE   STUS ='1'
    AND EMP_CODE = #{empCode};