by 커피요쿠르트d [SQL Query] outer join 아우터조인 조회조건 조건 아우터 조인 [2014.02.24 14:15:22]
WITH TBL1 AS( SELECT '111' AS KEY1, '2013' AS YEAR FROM DUAL UNION ALL SELECT '222' AS KEY1, '2013' AS YEAR FROM DUAL UNION ALL SELECT '333' AS KEY1, '2013' AS YEAR FROM DUAL UNION ALL SELECT '444' AS KEY1, '2013' AS YEAR FROM DUAL ), TBL2 AS ( SELECT '111' AS KEY1, '2013' AS YEAR, 'Y' AS FLAG FROM DUAL UNION ALL SELECT '444' AS KEY1, '2013' AS YEAR, 'N' AS FLAG FROM DUAL ) SELECT A.KEY1, A.YEAR, A.FLAG FROM TBL1 A, TBL2 B WHERE A.KEY1 = B.KEY1(+) AND B.FLAG(+) LIKE :PARAM ||'%' ;--
WITH TBL1 AS( SELECT '111' AS KEY1, '2013' AS YEAR FROM DUAL UNION ALL SELECT '222' AS KEY1, '2013' AS YEAR FROM DUAL UNION ALL SELECT '333' AS KEY1, '2013' AS YEAR FROM DUAL UNION ALL SELECT '444' AS KEY1, '2013' AS YEAR FROM DUAL ), TBL2 AS ( SELECT '111' AS KEY1, '2013' AS YEAR, 'Y' AS FLAG FROM DUAL UNION ALL SELECT '444' AS KEY1, '2013' AS YEAR, 'N' AS FLAG FROM DUAL ) SELECT A.KEY1, A.YEAR , B.FLAG FROM TBL1 A, TBL2 B WHERE A.KEY1 = B.KEY1(+) AND ( ( :PARAM IS NULL AND 1 = 1 ) -- 넘어오는 값이 없으면 필터링 없이.. OR ( :PARAM IS NOT NULL AND B.FLAG = :PARAM) -- 넘어오는 값있으면 그 값으로 필터링 ) ;
WITH TBL1 AS( SELECT '111' AS KEY1, '2013' AS YEAR FROM DUAL UNION ALL SELECT '222' AS KEY1, '2013' AS YEAR FROM DUAL UNION ALL SELECT '333' AS KEY1, '2013' AS YEAR FROM DUAL UNION ALL SELECT '444' AS KEY1, '2013' AS YEAR FROM DUAL ), TBL2 AS ( SELECT '111' AS KEY1, '2013' AS YEAR, 'Y' AS FLAG FROM DUAL UNION ALL SELECT '444' AS KEY1, '2013' AS YEAR, 'N' AS FLAG FROM DUAL ) SELECT * FROM ( SELECT A.KEY1 , A.YEAR , B.FLAG , NVL(B.FLAG, 'FALSE') AS IS_EXIST -- TBL2에 자료가 있는지 확인 FROM TBL1 A, TBL2 B WHERE A.KEY1 = B.KEY1(+) )WHERE IS_EXIST <> 'FALSE' ;