非常笨的一个方法,不知是不是这个意思:
SQL> with tmp as (
2select '200901' dat, 'C1' typ,10 st,1 inc, 2 exp, 9 remain from dual
3union
4select '200901' dat,'C2' typ,10 st,1 inc, 3 exp, 8 remain from dual
5union
6select '200902' dat, 'C1' typ,9 st,0 inc, 2 exp, 7 remain from dual
7union
8select '200902' dat,'C2' typ,8 st,1 inc, 2 exp, 7 remain from dual
9union
10select '200903' dat, 'C1' typ,7 st,3 inc, 1 exp, 9 remain from dual
11union
12select '200903' dat,'C2' typ,7 st,2 inc, 2 exp, 7 remain from dual)
13select t1.dat, t2.dat, t1.st, t3.inc, t3.exp, t2.remain
14from (select dat, typ, st from tmp where dat = '&from_c') t1,
15 (select dat, typ, remain from tmp where dat = '&to_c') t2,
16 (select typ, sum(inc) inc, sum(exp) exp
17
from tmp
18 where to_date(dat, 'yyyymm') between to_date('&from_c', 'yyyymm') and
19
to_date('&to_c', 'yyyymm')
20 group by typ) t3
21 where t1.typ = t2.typ
22 and t1.typ = t3.typ
23;
DATDAT
STINCEXP REMAIN
------ ------ ---------- ---------- ---------- ----------
200901 200903 10
4
5
9
200901 200903 10
4
7
7
|