要求股票5日的成交量的移动平均,就是当日,和上面四个交易日的成交量的平均
需要的数据我塞到临时表[t_avg]中了
stockid,tradenum,tradedate
测试数据是从4月7日开始的因为交易日期不连续,我用row_number()得到一个连续的比较系数,然后再:with base as
(
select t.*,row_number() over(partition by stockid order by tradedate) as rn from t_avg t
)select a.stockid,a.tradenum,a.tradedate,a.rn
from base a,base b
where a.tradedate>='2010-04-13 00:00:00.000' and b.tradedate>='2010-04-13 00:00:00.000'
and a.stockid=b.stockid
and a.rn>=b.rn and a.rn<b.rn+5
order by stockid,tradedate可是结果的tradenum字段,tradedate字段对于都是重复的...我很困惑啊000001 82687425 2010-04-19 00:00:00.000 9
000001 82687425 2010-04-19 00:00:00.000 9
000001 82687425 2010-04-19 00:00:00.000 9
000001 82687425 2010-04-19 00:00:00.000 9
000001 82687425 2010-04-19 00:00:00.000 9
000001 43513433 2010-04-20 00:00:00.000 10
000001 43513433 2010-04-20 00:00:00.000 10
000001 43513433 2010-04-20 00:00:00.000 10
000001 43513433 2010-04-20 00:00:00.000 10
000001 43513433 2010-04-20 00:00:00.000 10
000001 37358846 2010-04-21 00:00:00.000 11
000001 37358846 2010-04-21 00:00:00.000 11
000001 37358846 2010-04-21 00:00:00.000 11
000001 37358846 2010-04-21 00:00:00.000 11
000001 37358846 2010-04-21 00:00:00.000 11应该可以得到这5天的各自的tradenum和tradedate值的啊