Question : calculating moving average

The Q has been asked many time in EE and on google, I seen many solutions which looked to me very complicated. I had an idea where I thought would be an easier way, but it doesn't work right now and secondly, the talk about fastness on
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=93911
has made me insecure as well.

I fill a tmpTbl with GroundData like 'Date, MainIndex, Quote (Stand)' and after in a second attempt would like to fill my field 'Variable_1' with the MovingAverage of the last 200 days back from each specific date.

I tried following code but I get an error near 'as'

any idea what's wrong and any input for a probably more effienct way. What I don't know as well how to avoid a calculation when there are no 200 days back from the date, like the tmptbl starts on Jan. 1, it can't calculate a moving average on Jan 2 as there are no 200 days back.

Appreciate any feedback
Rgds
Kongta
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
UPDATE tblIndexTmp
	SET Variable_1 = x.AvgPx
	
	FROM         tblIndexTmp AS y INNER JOIN
                          (SELECT     Datum, MainIndex, AVG(Stand) AS AvgPx
                            FROM          tblIndexTmp
                            WHERE     (Datum BETWEEN dateadd(dd, -1,Datum) AND dateadd(dd, -1,Datum - 200)) AS x 
                            ON y.Datum = x.Datum AND y.MainIndex = x.MainIndex
							WHERE (y.UserName = @UserName)

Answer : calculating moving average

I am clueless on why its not updating where I am able to see the updated values at my end. What is your database? Are you checking in DB itself or any front end application?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
declare @tblIndexTmp as table(Datum datetime,MainIndex nvarchar(50),Stand float,Variable_1 float,UserName nvarchar(50))
insert into @tblIndexTmp values('1999-01-01',      'SX5E',      3342.32,null,'Kongta')
insert into @tblIndexTmp values('1999-01-04',      'SX5E',      3543.1 ,null,'Kongta')
insert into @tblIndexTmp values('1999-01-05',      'SX5E',      3604.67, null,'Kongta')
insert into @tblIndexTmp values('1999-01-06',      'SX5E',      3685.36, null,'Kongta')
insert into @tblIndexTmp values('1999-01-07',      'SX5E',      3627.87, null,'Kongta')
insert into @tblIndexTmp values('1999-01-08',      'SX5E',      3616.57, null,'Kongta')
insert into @tblIndexTmp values('1999-01-11',      'SX5E',      3546.4 , null,'Kongta')

declare @UserName nvarchar(50)
set @UserName = 'Kongta'
update t1
  set t1.Variable_1 = t3.Variable_1
 from @tblIndexTmp t1
 join (select Datum,MainIndex,
              (select avg(Stand) from @tblIndexTmp t1 
                where t1.Datum between dateadd(dd,-200,t2.Datum) and dateadd(dd,-1,t2.Datum) 
                  and datepart(weekday,t1.Datum) not in (6,7)
                  and t1.UserName = @UserName) Variable_1
        from @tblIndexTmp t2) t3
   on t1.Datum = t3.Datum and t1.MainIndex = t3.MainIndex
where t1.UserName = @UserName

select * from @tblIndexTmp
/*
1999-01-01 00:00:00.000	SX5E	3342.32	NULL	Kongta
1999-01-04 00:00:00.000	SX5E	3543.1	NULL	Kongta
1999-01-05 00:00:00.000	SX5E	3604.67	3543.1	Kongta
1999-01-06 00:00:00.000	SX5E	3685.36	3573.885	Kongta
1999-01-07 00:00:00.000	SX5E	3627.87	3611.04333333333	Kongta
1999-01-08 00:00:00.000	SX5E	3616.57	3615.25	Kongta
1999-01-11 00:00:00.000	SX5E	3546.4	3615.25	Kongta
*/
Random Solutions  
 
programming4us programming4us