SQL描述(4)

接前一篇用一句SQL计算出进口价格定基价格指数,这一篇将介绍一句SQL搞定 2个指标在不同时差范围内的相关性系数。

公式如下:Y ,X 分别表示2个指标,这2个指标必须有可比性,最齐码变化频率一样。比如都是一月一次的数据。

L表示时差,即X,Y差L个月。

代码的样子:

-----L  以参数的形式传入,从0 开始到 L的正向序列 
with   lv   as (select LEVEL-1 lv from dual CONNECT BY LEVEL <=11+1), 
       ---C0A0101 cpi 基准指标
       xy   as (select indi_date,max(case when indi_id='C0A0101' then indi_value else null end) as y,
                       ----C0A010D rpi 可选参数  【工业生产者出厂价格指数(PPI同比) C0A0201】【M1同比增速  C070104】
                               max(case when indi_id='C0A010D' then indi_value else null end) as x
              from indi_value where indi_id in ('C0A0101','C0A010D') and indi_date between '20130101' and '20130731'
              group by indi_date),
       --获取均值   cpi 均值 & rpi 均值
      xyavg as ( select avg(y) as ya,avg(x) as xa from xy where y is not null and x is not null), 
      fxy  as (select  indi_date, y,x,
                       --根据lv用lag函数进行错位
                       lag(y,lv) OVER(partition  by lv order by  indi_date ) as yy,
                       lag(x,lv) OVER(partition  by lv order by  indi_date ) as xx,
                      (select ya from xyavg ) as ya,(select xa from xyavg) as xa,lv
               from xy join lv on 1=1 where y is not null and x is not null)  
      --L>0 时 y 从第一个元素开始 x 从第 1+L个元素开始
      select lv as L, sum((xx-xa)*(y-ya))/sqrt(sum((xx-xa)*(xx-xa))*sum((y-ya)*(y-ya))) as r
      from fxy where yy is not null and xx is not null
      group by lv 
      union
      --L<0 时,x 从一个元素开始,y 从第1-L个元素开始      
      select -lv as L, sum((x-xa)*(yy-ya))/sqrt(sum((x-xa)*(x-xa))*sum((yy-ya)*(yy-ya))) as r
      from fxy where yy is not null and xx is not null
      group by lv 

上面的公式最主要的东西是变量n,n又和L有关系。n隐含在将指标按数据日期排序后的先后顺序中。


这一系列经济指标分析所用的公式都是一位高人提供。我只是用sql重写了一遍。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值