WITH 절
- 같은 쿼리 블럭이 두 번 이상 반복해서 사용된다면 위드 절을 이용해 가독과 편의성을 높일 수 있다.
- 쿼리 블록의 결과를 추출해서 유저의 임시 테이블 스페이스에 저장한다.
- 성능 향상
WITH COST_SUM AS ( SELECT A.EMP_ID, SUM(A.SALARY) AS SAL_TOT FROM EMPA ),
COST_AVG AS( SELECT SUM(DEPT(TOTAL)/COUNT(*) AS DAVG FROM COST )
SELECT * FROM COST_SUM WHERE SAL_TOT > (SELECT DAVG FROM COST_AVG )
ORDER BT EMP_ID;
-> 두 개의 서브 쿼리를 COST_SUM와 COST_AVG라는 테이블 처럼 사용하고 있다.
WITH 별칭 AS (서브쿼리) 메인쿼리
응용
WITH DATA AS
(
SELECT '5130143' AS CD,FU_F_ACCT_NAME('51301','') AS CN FROM DUAL UNION ALL
SELECT '5130143' AS CD,FU_F_ACCT_NAME('51301','') AS CN FROM DUAL UNION ALL
SELECT '5130143' AS CD,FU_F_ACCT_NAME('51301','') AS CN FROM DUAL UNION ALL
SELECT '5130143' AS CD,FU_F_ACCT_NAME('51301','') AS CN FROM DUAL UNION ALL
SELECT '5130187' AS CD,FU_F_ACCT_NAME('51301','') AS CN FROM DUAL UNION ALL
SELECT '1331911' AS CD,FU_F_ACCT_NAME('13319','') AS CN FROM DUAL UNION ALL
SELECT '5334104' AS CD,FU_F_ACCT_NAME('53341','') AS CN FROM DUAL
)
SELECT * FROM DATA
;
참고 사이트 : http://www.cyworld.com/konsanchi/2327970