select
Date, [Close],
avg([close]) over (order by date asc rows 4 preceding) as MA10
from
dbo.APPL
This query returns the rolling 4 day average of stock prices. However I want to show the values as NULL
if preceding days are fewer than 4.
How do I adjust that in SQL Server?
I think JeffUK meant doing something like this:
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