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
- SQL描述(4)
- SQL 错误代码描述
- SQL数据类型详细描述
- PL/SQL游标描述
- sql 性能综合描述
- SQL 描述(1)
- SQL描述(2)
- SQL描述(3)
- 获取sql server栏位描述
- sql server2005取得表描述
- sql 给表添加描述
- Hive简单SQL语句描述
- 显示表结构描述的Sql语句
- 資訊結構描述檢視 (Transact-SQL)
- oracle-ora 各种sql异常描述
- SQLSTATE SQL SERVER 驱动程序错误描述
- sql server 为表,列 增加描述
- oracle-ora 各种sql异常描述
- Minesweeper UVA- 这尼玛坑爹的空行
- Win7 CreateRemoteThread 另类使用方法
- 黑马程序员高级视频学习笔记IO输入输出流File类
- $(this).index() 值一直为-1
- Hibernate逍遥游记——(一)访问关系数据库
- SQL描述(4)
- 【c++】error 某方法 declared as a 'virtual' field
- "[ERROR] Could not find git path"
- centos6 安装chrome,wps
- [百度百科]英语状语
- oracle通过impdp导入不同表用户、不同表空间的数据
- Win7无线网卡分享wifi不稳定,手机连接总是自动断开,换这个网卡驱动就OK啦
- linux tty pty pts 概念 区别
- 二叉树非递归遍历