oracle同环比计算
with temp as
(select to_char(jsrq , 'yyyymm') as month, sum(zfy ) as num
from hip_mz_ys a, JCXX_JG b, jcxx_ks c, jcxx_ry d
where a.jg_id = b.yljgdm
and a.bm_id = c.ksid
and a.ry_id = d.ylryid
group by to_char(jsrq , 'yyyymm'))
select month,
round(nvl(num * 100 / (select num
from temp t1
where t1.month = (substr(t.month, 1, 4) - 1) ||
substr(t.month, -2)),
0),
2) as 同比,
round(nvl(num * 100 /
(select num
from temp t2
where t2.month =
to_char(to_date(t.month, 'yyyymm') - 1, 'yyyymm')),
0),
2) as 环比
from temp t
order by month asc