我有两个临时表,我从存储过程中提取数据
#rtn
看起来像
dt indx_nm cusip_id lrtn
2013-08-19 00:00:00.000 ABQI 33736Q104 -10.2
2013-08-16 00:00:00.000 ABQI 33736Q104 2.28
2013-08-15 00:00:00.000 ABQI 33736Q104 -18.38
2013-08-14 00:00:00.000 ABQI 33736Q104 -2.73
2013-08-13 00:00:00.000 ABQI 33736Q104 3.38
2013-08-12 00:00:00.000 ABQI 33736Q104 2.45
2013-08-19 00:00:00.000 ACNACTR 18385P101 -0.13
2013-08-16 00:00:00.000 ACNACTR 18385P101 -0.02
2013-08-15 00:00:00.000 ACNACTR 18385P101 -0.51
2013-08-14 00:00:00.000 ACNACTR 18385P101 -0.37
2013-08-13 00:00:00.000 ACNACTR 18385P101 5.2
2013-08-12 00:00:00.000 ACNACTR 18385P101 6.31
等wuth有很多不同的indx_nm
和cusip_id
#rtn2
看起来像
dt cusip_id lrtn
2013-08-19 00:00:00.000 00162Q106 -0.00752860307693481
2013-08-16 00:00:00.000 00162Q106 -0.0131599903106689
2013-08-15 00:00:00.000 00162Q106 -0.0369237661361694
2013-08-14 00:00:00.000 00162Q106 -0.0315451622009277
2013-08-13 00:00:00.000 00162Q106 -0.0180829167366028
2013-08-12 00:00:00.000 00162Q106 -0.0140992403030396
2013-08-19 00:00:00.000 00162Q205 -0.00420033931732178
2013-08-16 00:00:00.000 00162Q205 -0.00409352779388428
2013-08-15 00:00:00.000 00162Q205 -0.023819625377655
2013-08-14 00:00:00.000 00162Q205 -0.0221214294433594
2013-08-13 00:00:00.000 00162Q205 -0.0169548392295837
2013-08-12 00:00:00.000 00162Q205 -0.00570738315582275
还有更多不同的indx_nm
和cusip_id
我目前的代码是:
insert into etf_table (indx_nm, cusip_id, cor, dt_pnts)
select a.indx_nm, b.cusip_id,
(AVG(a.lrtn * b.lrtn) - AVG(a.lrtn)*AVG(b.lrtn))/ STDEVP(a.lrtn)/STDEVP(b.lrtn) cor, COUNT(*) dt_pnts
from #rtn a, #rtn2 b
where a.dt = b.dt and a.lrtn is not null and b.lrtn is not null
group by a.indx_nm, b.cusip_id
having STDEVP(a.lrtn) > 0 and STDEVP(b.lrtn) > 0
order by a.indx_nm, cor desc
但是,我只想根据名为(AVG(a.lrtn * b.lrtn) - AVG(a.lrtn)*AVG(b.lrtn))/ STDEVP(a.lrtn)/STDEVP(b.lrtn)
etf_data
Ticker CUSIP_ID Indx
AADR 00768Y206 MXEA
AAIT 46429B374 MSIAITNU
AAXJ 464288182 NDUECAXJ
ABCS 18383Q788 BKABCT
(明显多了ticker
,cusip_id
和indx
)
条件是:
indx_nm
中的#rtn
与此表格中的Indx
相匹配,#rtn2中的cusip_id
与此表格中cusip_id
的每一行相匹配
即仅对(AVG(a.lrtn * b.lrtn) - AVG(a.lrtn)*AVG(b.lrtn))/ STDEVP(a.lrtn)/STDEVP(b.lrtn)
= cusip_id
和00768Y206
= indx
执行MXEA
等等
对于cusip_id
中给出的每个对应indx
和etf_data
谢谢!
答案 0 :(得分:0)
尝试以下方法:
INSERT INTO etf_table (indx_nm, cusip_id, cor, dt_pnts)
SELECT a.indx_nm, b.cusip_id, (AVG(a.lrtn * b.lrtn) - AVG(a.lrtn) * AVG(b.lrtn)) / STDEVP(a.lrtn) / STDEVP(b.lrtn) AS cor, COUNT(*) AS dt_pnts
FROM [#rtn] AS a
INNER JOIN [#rtn2] AS b ON a.dt = b.dt
INNER JOIN etf_data AS c ON a.indx_nm = c.Indx AND b.cusip_id = c.cusip.id
WHERE (a.lrtn IS NOT NULL) AND (b.lrtn IS NOT NULL)
GROUP BY a.indx_nm, b.cusip_id
HAVING (STDEVP(a.lrtn) > 0) AND (STDEVP(b.lrtn) > 0)
ORDER BY a.indx_nm, cor DESC