Question : daily average of the last 200 data

I do have following table

Datum      MainIndex      Stand           UserName
1999-01-01      SX5E      3342.32
1999-01-02      SX5E      3342.32
1999-01-03      SX5E      3342.32
1999-01-04      SX5E      3543.1
1999-01-05      SX5E      3604.67
1999-01-06      SX5E      3685.36
1999-01-07      SX5E      3627.87
1999-01-08      SX5E      3616.57
1999-01-09      SX5E      3616.57
1999-01-10      SX5E      3616.57
1999-01-11      SX5E      3546.4
1999-01-12      SX5E      3502.38
1999-01-13      SX5E      3336.45
1999-01-14      SX5E      3365.43
1999-01-15      SX5E      3426.06
1999-01-16      SX5E      3426.06
1999-01-17      SX5E      3426.06
1999-01-18      SX5E      3515.43

....

where I would like to calculate the Average on each day of the past 200 entries (including calculation day, so back 199 entries + 1 current). I tried

SELECT t3.Variable_1
 from tblIndexTmp t1
 join (select Datum,MainIndex,
              (select Top 200 avg(isnull(Stand,0)) from tblIndexTmp t1  
                WHERE t1.UserName = @UserName) Variable_1
        from tblIndexTmp t2) t3
   on t1.Datum = t3.Datum and t1.MainIndex = t3.MainIndex
where t1.UserName = UserName

which gave  back always the same average on each day. Any idea how to solve?
thx
Kongta

Answer : daily average of the last 200 data

Wait, I put all your data into my test and I see what my problem was, where I said RowNo-200 before, it should be RowNo-199.  I now get the same values as you on those last two rows.  
Sorry about all the back and forth, it is hard to debug a situation in this posting back and forth environment unless you get a full test dataset and expected results. :)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
;WITH baseTable   
as  
(  
select Datum, MainIndex, Stand, UserName, ROW_NUMBER() OVER (ORDER BY datum ASC) AS RowNo  
from tblIndexTmp  
),  
t3 as   
(  
SELECT b1.Datum, b1.MainIndex, b1.Stand, b1.UserName, AVG(b2.Stand) AvgStand  
FROM baseTable b1  
INNER JOIN baseTable b2 ON b1.UserName = b2.UserName AND b1.MainIndex = b2.MainIndex AND b2.RowNo BETWEEN b1.RowNo-199 AND b1.RowNo  
WHERE b1.UserName = @username  
GROUP BY b1.Datum, b1.MainIndex, b1.Stand, b1.UserName  
)  
update t1   
  set t1.Variable_1 = t3.AvgStand   
 from tblIndexTmp t1  
 INNER JOIN t3 ON t1.UserName = t3.UserName AND t1.MainIndex = t3.MainIndex AND t1.Datum = t3.Datum
Random Solutions  
 
programming4us programming4us