title: How to create a relative range standard deviation (stdev) function in SQL Server (T-SQL) without using loops?
body: <p>I'm trying to come up with a way to use a relative stdev range function in SQL Server 2005/8 without any looping. Assuming a test dataset (test_data) with 10 records and columns:</p>
  
  <pre><code>column_id identity(1,1), column_data, column_stdev
  </code></pre>
  
  <p>I would need my stdev() to work as follows:  </p>
  
  <p>stdev(column_data where id between i and i + 3) with i = column_id, autoincrementing.  </p>
  
  <p>So far I'm only able to acoomplish it via the following query, which is extra slow:</p>
  
  <pre><code>declare @i int
  set @i=1
  while @i &lt; 11
   begin
   update test_data
   set column_stdev = (select stdev(column_data) from test_data 
                       where column_id between @i and @i+2)
   set @i=@i+1
  end
  </code></pre>
  
  <p>Could you, please, let me know if there is a way to avoid looping in order to set column_stdev to get stdev of the past 3 records in column_data.</p>