마농님 안녕하세요! 댓글감사합니다
너무 길어서 간략하게 적었습니다 ㅠ
이렇게 적으면 될까요?
select '1' as seq
, chain_cd
, sum(r_amt) as amt
from a
group by chain_cd
union all
select '2' as seq
, chain_cd
, sum(t_amt) as amt
from b
group by chain_cd
union all
select '3' as seq
, chain_cd
, sum(p_amt) as amt
from c
group by chain_cd
order by chain_cd, seq
-- Oracle : Partition Outer Join -- SELECT b.chain_cd , a.seq , NVL(b.amt, 0) amt FROM (SELECT LEVEL seq FROM dual CONNECT BY LEVEL <= 3) a LEFT OUTER JOIN (SELECT 1 seq , chain_cd , SUM(r_amt) amt FROM a GROUP BY chain_cd UNION ALL SELECT 2 seq , chain_cd , SUM(t_amt) amt FROM b GROUP BY chain_cd UNION ALL SELECT 3 seq , chain_cd , SUM(p_amt) amt FROM c GROUP BY chain_cd ) b PARTITION BY (b.chain_cd) ON a.seq = b.seq ORDER BY chain_cd, seq ;
WITH tmp AS ( SELECT 1 seq , chain_cd , SUM(r_amt) amt FROM a GROUP BY chain_cd UNION ALL SELECT 2 seq , chain_cd , SUM(t_amt) amt FROM b GROUP BY chain_cd UNION ALL SELECT 3 seq , chain_cd , SUM(p_amt) amt_3 FROM c GROUP BY chain_cd ) SELECT a.chain_cd , b.seq , NVL(c.amt, 0) amt FROM (SELECT DISTINCT chain_cd FROM tmp) a CROSS JOIN (SELECT LEVEL seq FROM dual CONNECT BY LEVEL <= 3) b LEFT OUTER JOIN tmp c ON a.chain_cd = c.chain_cd AND b.seq = c.seq ORDER BY chain_cd, seq ;