1

我有两个临时表,我正在从存储过程中提取数据

#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

等等更多的不同indx_nmcusip_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  

(更多tickercusip_id而且indx很明显)

条件是:in
indx_nm匹配#rtnIndx表中的 in,其中in # cusip_idrtn2cusip_id沿每一行匹配此表中 的
(AVG(a.lrtn * b.lrtn) - AVG(a.lrtn)*AVG(b.lrtn))/ STDEVP(a.lrtn)/STDEVP(b.lrtn)cusip_id00768Y206indxMXEAcusip_idindxetf_data

4

1 回答 1

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
于 2013-08-23T00:16:50.430 回答