SQL 服务器滚动平均值
SQL Server rolling average
select
Date, [Close],
avg([close]) over (order by date asc rows 4 preceding) as MA10
from
dbo.APPL
此查询returns 股票价格的 4 天滚动平均值。但是,如果前几天少于 4,我想将值显示为 NULL
。
如何在 SQL 服务器中调整它?
我认为 JeffUK 的意思是做这样的事情:
select
Date, [Close],
case when days >= 4 then MA10 end as MA10
from
(
select
Date, [Close],
avg([close]) over (order by date asc rows 4 preceding) as MA10,
count([close]) over (order by date asc rows 4 preceding) as days
from
dbo.APPL
) X
select
Date, [Close],
avg([close]) over (order by date asc rows 4 preceding) as MA10
from
dbo.APPL
此查询returns 股票价格的 4 天滚动平均值。但是,如果前几天少于 4,我想将值显示为 NULL
。
如何在 SQL 服务器中调整它?
我认为 JeffUK 的意思是做这样的事情:
select
Date, [Close],
case when days >= 4 then MA10 end as MA10
from
(
select
Date, [Close],
avg([close]) over (order by date asc rows 4 preceding) as MA10,
count([close]) over (order by date asc rows 4 preceding) as days
from
dbo.APPL
) X