title: T-SQL calculate moving average
body: <p>I am working with SQL Server 2008 R2, trying to calculate a moving average. For each record in my view, I would like to collect the values of the 250 previous records, and then calculate the average for this selection.</p>
  
  <p>My view columns are as follows:</p>
  
  <pre><code>TransactionID | TimeStamp           | Value | MovAvg
  ----------------------------------------------------
              1 | 01.09.2014 10:00:12 |     5 |    
              2 | 01.09.2014 10:05:34 |     3 | 
  ...
            300 | 03.09.2014 09:00:23 |     4 | 
  </code></pre>
  
  <p><code>TransactionID</code> is unique. For each <code>TransactionID</code>, I would like to calculate the average for column value, over previous 250 records. So for TransactionID 300, collect all values from previous 250 rows (view is sorted descending by TransactionID) and then in column MovAvg write the result of the average of these values. I am looking to collect data within a range of records.</p>