{column:width=50%}
select yyyymm, nvl(sum(inp_amt),0), nvl(sum(out_amt),0)
from (select MANAGE_NUM, substr(INPUT_DATE,1,6) yyyymm,
AMT inp_amt, to_number(null) out_amt
from INPUT
union all
select MANAGE_NUM, substr(OUTPUT_DATE,1,6) yyyymm,
to_number(null) inp_amt, AMT out_amt
from OUTPUT)
where yyyymm between substr('20060301',1,6) and substr('20060430',1,6)
group by yyyymm;
YYYYMM NVL(SUM(INP_AMT),0) NVL(SUM(OUT_AMT),0)
------------ ------------------- -------------------
200604 2000 2000
200603 7000 7000
Elapsed: 00:00:00.04
{column}{column:width=50%}
Execution Plan
----------------------------
Plan hash value: 601556075
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 18 | 612 | 7 (15)| 00:00:01 |
| 1 | HASH GROUP BY | | 18 | 612 | 7 (15)| 00:00:01 |
| 2 | VIEW | | 18 | 612 | 6 (0)| 00:00:01 |
| 3 | UNION-ALL | | | | | |
|* 4 | TABLE ACCESS FULL| INPUT | 9 | 171 | 3 (0)| 00:00:01 |
|* 5 | TABLE ACCESS FULL| OUTPUT | 9 | 171 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
-------------------------------------------------------------------------------------------
4 - filter(SUBSTR("INPUT_DATE",1,6)>='200603' AND SUBSTR("INPUT_DATE",1,6)<='200604')
5 - filter(SUBSTR("OUTPUT_DATE",1,6)>='200603' AND SUBSTR("OUTPUT_DATE",1,6)<='200604')
{column}