select b.class,b.name,b.date,avg(b.clo_2) as MA5
From (
select t1.*,t2.clo as CLO_2 from data_RN t1 inner join data_RN t2 on t1.name= t2.name and t2.rn<=t1.rn and t2.rn>=t1.rn-4
)b
group by b.class,b.name,b.rn,b.date
Order by name,date desc
3.现在的问题是,如何讲MA5到MA60一次性的计算出来呢? 如果不用T-SQL直接计算,这种情况是不是应该使用SSIS?Thanks in advance,
------解决方案--------------------
--如果上市公司名叫name,要从一个总表中得到各股票的均价,则
select name,
(select avg(close) from(select top 5 close from tb where name=a.name order by date desc)t) as MA5,
(select avg(close) from(select top 10 close from tb where name=a.name order by date desc)t) as MA10,
(select avg(close) from(select top 20 close from tb where name=a.name order by date desc)t) as MA20,
(select avg(close) from(select top 30 close from tb where name=a.name order by date desc)t) as MA30,
(select avg(close) from(select top 60 close from tb where name=a.name order by date desc)t) as MA60
from tb a