Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3666628
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:54:13+00:00 2026-05-19T01:54:13+00:00

I’m trying to come up with a way to use a relative stdev range

  • 0

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:

column_id identity(1,1), column_data, column_stdev

I would need my stdev() to work as follows:

stdev(column_data where id between i and i + 3) with i = column_id, autoincrementing.

So far I’m only able to acoomplish it via the following query, which is extra slow:

declare @i int
set @i=1
while @i < 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

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:54:13+00:00Added an answer on May 19, 2026 at 1:54 am

    One obvious improvement would be to get rid of the WHILE loop and the row by row processing. If you are not doing it in an explicit transaction you will need to wait for 10 individual commits to complete and the total amount of work required is greater anyway as shown by the stats below.

    NB: I’m not claiming that my query can’t be improved upon however. You may be able to adjust the “quirky update” approach for your needs and do it with a single scan through the data for example.

    create table t (
    column_id int identity(1,1) primary key, 
    column_data float, 
    column_stdev float)
    
    insert into t (column_data)
    select top 10 CHECKSUM(newid()) from sys.objects
    
    SET STATISTICS IO ON
    
    UPDATE t 
    SET  column_stdev = (SELECT stdev(t2.column_data)
                         FROM t t2 
                         WHERE t2.column_id BETWEEN t.column_id AND t.column_id + 2)
    
    /*Table 't'. Scan count 11, logical reads 42, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    Table 'Worktable'. Scan count 1, logical reads 23, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.*/
    
    declare @i int
    set @i=1
    while @i < 11
     begin
     update t
     set column_stdev = (select stdev(column_data) from t 
                         where column_id between @i and @i+2)
     set @i=@i+1
    end
    
    
    /* (Aggregated the 10 results)
    Table 't'. Scan count 20, logical reads 240, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    Table 'Worktable'. Scan count 10, logical reads 230, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.*/
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.