接上一期,这次要分析的是IPI,带有基期的进口价格指数。相当于价格水平的一个绝对衡量。
公式如下;需要的数据为:进口价格环比增速(IPI_m),进口价格同比增速(IPI_y),以10年的为基期举例说明
首先需要计算出10年各月份的月度定基数据序列。然后分别计算大于基期,小于基期的定基价格指数。
这些计算都需要各行计算来完成。代码如下:
----计算根据环比数据计算 2010年1月=100的月度定基数据序列,错一位,日期升序,1月份的位置会被空出来,nvl 转为100就可以了 with base10 as ( select indi_date,indi_value from indi_value where indi_id='C0A0102' and indi_date between '20100101' and '20101231' -- 获取10年的数据 ), up10 as (select indi_date,indi_value,case when indi_date like '201001%' then 100 else null end as v,rank() OVER(order by indi_date) as d from base10),---处理10年的数据按日期升序,并且使基期=100,往后的值都为空,然后通过行间计算,进行填补 upx10 as (select d,v from up10 model dimension by (d) measures (indi_value,v) RULES UPDATE ( V[d>1]=v[cv(d)-1]*(1+(indi_value[cv(d)]-100)/100) --大于基期的计算公式 ) ), ipi10 as (select u.indi_date,x.v from upx10 x join up10 u on x.d=u.d), base as ( select indi_date,indi_value from indi_value where indi_id='C050109' and indi_date between '19800101' and '20140101' ),---获取指定时间区间内的进出同比数据 up as(select 'P' as P,u.indi_date,indi_value,v,rank() OVER(order by u.indi_date) as d ---将同比数据 ipiUP 结果集和 base10 结果集整合到一起,同时添加 P,D这样的方便model运算的列 from base u left join ipi10 on u.indi_date=ipi10.indi_date where u.indi_date between '20100101' and '20140101'), --p 会被带入 partition by 将所有数据分成一组,d 被带入dimension by 当作纬度,纬度通俗的理解就是数组下标, upx as (select d,v,indi_value from up model partition by (p) dimension by (d) measures (indi_value,v) RULES UPDATE ( V[d>12]=v[cv(d)-12]*(1+(indi_value[cv(d)]-100)/100) ) ),--modle 行间运算,下面是小于基期的数据运算,原理相同,但要注意,日期是倒序 down as(select 'P' as P,u.indi_date,indi_value,v,rank() OVER(order by u.indi_date desc) as d from base u left join ipi10 on u.indi_date=ipi10.indi_date where u.indi_date between '19800101' and '20101231' order by u.indi_date desc ), downx as (select d,v,indi_value from down model partition by (p) dimension by (d) measures (indi_value,v) RULES UPDATE ( V[d>12]=v[cv(d)-12]/(1+(indi_value[cv(d)]-100)/100) ) ) select d.indi_date,x.v from downx x join down d on x.d=d.d union select up.indi_date,x.v from upx x join up on x.d=up.d
有疑问的给我留言。