WITH code1 AS ( SELECT 1 cd1 FROM dual UNION ALL SELECT 2 FROM dual UNION ALL SELECT 3 FROM dual UNION ALL SELECT 4 FROM dual ) , code2 AS ( SELECT 1 cd2 FROM dual UNION ALL SELECT 2 FROM dual ) , data1 AS ( SELECT 1 cd1, 1 cd2, 10 v FROM dual UNION ALL SELECT 1, 2, 15 FROM dual UNION ALL SELECT 2, 1, 21 FROM dual UNION ALL SELECT 3, 2, 12 FROM dual UNION ALL SELECT 4, 1, 13 FROM dual UNION ALL SELECT 4, 2, 15 FROM dual ) SELECT c1.cd1 , c2.cd2 , NVL(d1.v, 0) v FROM code1 c1 CROSS JOIN code2 c2 LEFT OUTER JOIN data1 d1 ON c1.cd1 = d1.cd1 AND c2.cd2 = d1.cd2 ORDER BY cd1, cd2 ;
단순히 B코드만 누락되는 경우라면
WITH T AS ( SELECT '1' A, '1' B, 10 C FROM DUAL UNION ALL SELECT '1' A, '2' B, 15 C FROM DUAL UNION ALL SELECT '2' A, '1' B, 21 C FROM DUAL UNION ALL SELECT '3' A, '2' B, 12 C FROM DUAL UNION ALL SELECT '4' A, '1' B, 13 C FROM DUAL UNION ALL SELECT '4' A, '2' B, 15 C FROM DUAL ) SELECT LST.A, CD.B, NVL(LST.C,0) C FROM ( SELECT '1' B FROM DUAL UNION ALL SELECT '2' B FROM DUAL ) CD --혹은 B코드 테이블 LEFT OUTER JOIN T LST PARTITION BY (LST.A) ON (CD.B = LST.B)