2개의 쿼리는 동일 테이블과 동일 조건입니다.
단 하나는 UNION ALL 로 가져오고 하나는 풀아우터 조인 방식으로 가져 왔습니다.
데이터가 몃개 없어서 결과는 동일한대 혹시 결과가 다르게 나올수도 있을가요?
WHERE A.PJT = 'ZRE' AND TYPE = 'P' 이조건으로 데이터는 여러건 나옵니다.
SELECT RN, CLASS, MAX(C1) AS C1 , MAX(C2) AS C2 ,MAX(C3) AS C3, MAX(C4) AS C4 , MAX(C5) AS C5 , MAX(C6) AS C6 , MAX(C4) AS C7 , MAX(C5) AS C8 , MAX(C6) AS C9
FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY CLASS ORDER BY CLASS DESC) AS RN , CLASS
, FROM_S AS c1 , TO_S AS c2, DESCRT AS c3 , '' AS c4 , '' AS c5, '' AS c6 , '' AS c7 , '' AS c8, '' AS c9
FROM EMPT A
WHERE A.PJT = 'ZRE'
AND TYPE = 'P'
UNION ALL
SELECT ROW_NUMBER() OVER (PARTITION BY CLASS ORDER BY CLASS DESC) AS RN , CLASS
, '' AS c1 , '' AS c2, '' AS c3 , FROM_S AS c4 , TO_S AS c5, DESCRT AS c6 , '' AS c7 , '' AS c8, '' AS c9
FROM EMPT A
WHERE A.PJT = 'ZRE'
AND TYPE = 'F'
UNION ALL
SELECT ROW_NUMBER() OVER (PARTITION BY CLASS ORDER BY CLASS DESC) AS RN , CLASS
, '' AS c1 , '' AS c2, '' AS c3 , '' AS c4 , '' AS c5, '' AS c6 , FROM_S AS c7 , TO_S AS c8, DESCRT AS c9
FROM EMPT A
WHERE A.PJT = 'ZRE'
AND TYPE = 'B'
)
GROUP BY CLASS, RN
ORDER BY CLASS , RN
/////////////////////////////////
WITH
DTL AS (
SELECT ROW_NUMBER() OVER (PARTITION BY CLASS ORDER BY CLASS DESC) AS RN , CLASS
, FROM_S AS c1 , TO_S AS c2, DESCRT AS c3
FROM EMPT A
WHERE A.PJT = 'ZRE'
AND TYPE = 'P'
)
,
ING AS (
SELECT ROW_NUMBER() OVER (PARTITION BY CLASS ORDER BY CLASS DESC) AS RN , CLASS
, FROM_S AS c1 , TO_S AS c2, DESCRT AS c3
FROM EMPT A
WHERE A.PJT = 'ZRE'
AND TYPE = 'F'
)
,
VB AS (
SELECT ROW_NUMBER() OVER (PARTITION BY CLASS ORDER BY CLASS DESC) AS RN , CLASS
, FROM_S AS c1 , TO_S AS c2, DESCRT AS c3
FROM EMPT A
WHERE A.PJT = 'ZRE'
AND TYPE = 'B'
)
SELECT A.RN, A.CLASS, A.C1, A.C2, A.C3 , B.C1, B.C2, B.C3 , C.C1, C.C2, C.C3
FROM DTL A
FULL OUTER JOIN ING B ON A.RN = B.RN AND A.CLASS = B.CLASS
FULL OUTER JOIN VB C ON COALESCE(A.RN, B.RN) = C.RN AND COALESCE(A.CLASS, B.CLASS) = C.CLASS
ORDER BY A.CLASS , RN
실 데이터로 테스트 한건 아니라서 로직적으로 봤을때....
일단 데이터는 동일하게 나오는게 맞아 보입니다.
(하지만 제가 생각 못한 오류가 있을 수는 있습니다.)
첫번째 쿼리를 이용해 동일한 결과가 나오는 다른 쿼리도 작성해봤습니다.
(물론 이것도 정확하다고 확답은... 흠...)
SELECT RN, CLASS, MAX(C1) AS C1 , MAX(C2) AS C2 ,MAX(C3) AS C3, MAX(C4) AS C4 , MAX(C5) AS C5 , MAX(C6) AS C6 , MAX(C4) AS C7 , MAX(C5) AS C8 , MAX(C6) AS C9 FROM ( SELECT ROW_NUMBER() OVER (PARTITION BY CLASS, TYPE ORDER BY CLASS DESC) AS RN , CLASS , CASE WHEN TYPE = 'P' THEN FROM_S END AS c1 , CASE WHEN TYPE = 'P' THEN TO_S END AS c2 , CASE WHEN TYPE = 'P' THEN DESCRT END AS c3 , CASE WHEN TYPE = 'F' THEN FROM_S END AS c4 , CASE WHEN TYPE = 'F' THEN TO_S END AS c5 , CASE WHEN TYPE = 'F' THEN DESCRT END AS c6 , CASE WHEN TYPE = 'B' THEN FROM_S END AS c7 , CASE WHEN TYPE = 'B' THEN TO_S END AS c8 , CASE WHEN TYPE = 'B' THEN DESCRT END AS c9 FROM EMPT A WHERE A.PJT = 'ZRE' AND TYPE IN ('P', 'F','B') ) GROUP BY CLASS, RN ORDER BY CLASS, RN