SQL描述(4)

来源:互联网 发布:淘宝客佣金扣除红包吗 编辑:程序博客网 时间:2024/04/30 00:44

接前一篇用一句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
原创粉丝点击