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 702721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:45:08+00:00 2026-05-14T03:45:08+00:00

Are there any Linear Regression Function in SQL Server 2005/2008, similar to the the

  • 0

Are there any Linear Regression Function in SQL Server 2005/2008, similar to the the Linear Regression functions in Oracle ?

  • 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-14T03:45:08+00:00Added an answer on May 14, 2026 at 3:45 am

    To the best of my knowledge, there is none. Writing one is pretty straightforward, though. The following gives you the constant alpha and slope beta for y = Alpha + Beta * x + epsilon:

    -- test data (GroupIDs 1, 2 normal regressions, 3, 4 = no variance)
    WITH some_table(GroupID, x, y) AS
    (       SELECT 1,  1,  1    UNION SELECT 1,  2,  2    UNION SELECT 1,  3,  1.3  
      UNION SELECT 1,  4,  3.75 UNION SELECT 1,  5,  2.25 UNION SELECT 2, 95, 85    
      UNION SELECT 2, 85, 95    UNION SELECT 2, 80, 70    UNION SELECT 2, 70, 65    
      UNION SELECT 2, 60, 70    UNION SELECT 3,  1,  2    UNION SELECT 3,  1, 3
      UNION SELECT 4,  1,  2    UNION SELECT 4,  2,  2),
     -- linear regression query
    /*WITH*/ mean_estimates AS
    (   SELECT GroupID
              ,AVG(x * 1.)                                             AS xmean
              ,AVG(y * 1.)                                             AS ymean
        FROM some_table
        GROUP BY GroupID
    ),
    stdev_estimates AS
    (   SELECT pd.GroupID
              -- T-SQL STDEV() implementation is not numerically stable
              ,CASE      SUM(SQUARE(x - xmean)) WHEN 0 THEN 1 
               ELSE SQRT(SUM(SQUARE(x - xmean)) / (COUNT(*) - 1)) END AS xstdev
              ,     SQRT(SUM(SQUARE(y - ymean)) / (COUNT(*) - 1))     AS ystdev
        FROM some_table pd
        INNER JOIN mean_estimates  pm ON pm.GroupID = pd.GroupID
        GROUP BY pd.GroupID, pm.xmean, pm.ymean
    ),
    standardized_data AS                   -- increases numerical stability
    (   SELECT pd.GroupID
              ,(x - xmean) / xstdev                                    AS xstd
              ,CASE ystdev WHEN 0 THEN 0 ELSE (y - ymean) / ystdev END AS ystd
        FROM some_table pd
        INNER JOIN stdev_estimates ps ON ps.GroupID = pd.GroupID
        INNER JOIN mean_estimates  pm ON pm.GroupID = pd.GroupID
    ),
    standardized_beta_estimates AS
    (   SELECT GroupID
              ,CASE WHEN SUM(xstd * xstd) = 0 THEN 0
                    ELSE SUM(xstd * ystd) / (COUNT(*) - 1) END         AS betastd
        FROM standardized_data pd
        GROUP BY GroupID
    )
    SELECT pb.GroupID
          ,ymean - xmean * betastd * ystdev / xstdev                   AS Alpha
          ,betastd * ystdev / xstdev                                   AS Beta
    FROM standardized_beta_estimates pb
    INNER JOIN stdev_estimates ps ON ps.GroupID = pb.GroupID
    INNER JOIN mean_estimates  pm ON pm.GroupID = pb.GroupID
    

    Here GroupID is used to show how to group by some value in your source data table. If you just want the statistics across all data in the table (not specific sub-groups), you can drop it and the joins. I have used the WITH statement for sake of clarity. As an alternative, you can use sub-queries instead. Please be mindful of the precision of the data type used in your tables as the numerical stability can deteriorate quickly if the precision is not high enough relative to your data.

    EDIT: (in answer to Peter’s question for additional statistics like R2 in the comments)

    You can easily calculate additional statistics using the same technique. Here is a version with R2, correlation, and sample covariance:

    -- test data (GroupIDs 1, 2 normal regressions, 3, 4 = no variance)
    WITH some_table(GroupID, x, y) AS
    (       SELECT 1,  1,  1    UNION SELECT 1,  2,  2    UNION SELECT 1,  3,  1.3  
      UNION SELECT 1,  4,  3.75 UNION SELECT 1,  5,  2.25 UNION SELECT 2, 95, 85    
      UNION SELECT 2, 85, 95    UNION SELECT 2, 80, 70    UNION SELECT 2, 70, 65    
      UNION SELECT 2, 60, 70    UNION SELECT 3,  1,  2    UNION SELECT 3,  1, 3
      UNION SELECT 4,  1,  2    UNION SELECT 4,  2,  2),
     -- linear regression query
    /*WITH*/ mean_estimates AS
    (   SELECT GroupID
              ,AVG(x * 1.)                                             AS xmean
              ,AVG(y * 1.)                                             AS ymean
        FROM some_table pd
        GROUP BY GroupID
    ),
    stdev_estimates AS
    (   SELECT pd.GroupID
              -- T-SQL STDEV() implementation is not numerically stable
              ,CASE      SUM(SQUARE(x - xmean)) WHEN 0 THEN 1 
               ELSE SQRT(SUM(SQUARE(x - xmean)) / (COUNT(*) - 1)) END AS xstdev
              ,     SQRT(SUM(SQUARE(y - ymean)) / (COUNT(*) - 1))     AS ystdev
        FROM some_table pd
        INNER JOIN mean_estimates  pm ON pm.GroupID = pd.GroupID
        GROUP BY pd.GroupID, pm.xmean, pm.ymean
    ),
    standardized_data AS                   -- increases numerical stability
    (   SELECT pd.GroupID
              ,(x - xmean) / xstdev                                    AS xstd
              ,CASE ystdev WHEN 0 THEN 0 ELSE (y - ymean) / ystdev END AS ystd
        FROM some_table pd
        INNER JOIN stdev_estimates ps ON ps.GroupID = pd.GroupID
        INNER JOIN mean_estimates  pm ON pm.GroupID = pd.GroupID
    ),
    standardized_beta_estimates AS
    (   SELECT GroupID
              ,CASE WHEN SUM(xstd * xstd) = 0 THEN 0
                    ELSE SUM(xstd * ystd) / (COUNT(*) - 1) END         AS betastd
        FROM standardized_data
        GROUP BY GroupID
    )
    SELECT pb.GroupID
          ,ymean - xmean * betastd * ystdev / xstdev                   AS Alpha
          ,betastd * ystdev / xstdev                                   AS Beta
          ,CASE ystdev WHEN 0 THEN 1 ELSE betastd * betastd END        AS R2
          ,betastd                                                     AS Correl
          ,betastd * xstdev * ystdev                                   AS Covar
    FROM standardized_beta_estimates pb
    INNER JOIN stdev_estimates ps ON ps.GroupID = pb.GroupID
    INNER JOIN mean_estimates  pm ON pm.GroupID = pb.GroupID
    

    EDIT 2 improves numerical stability by standardizing data (instead of only centering) and by replacing STDEV because of numerical stability issues. To me, the current implementation seems to be the best trade-off between stability and complexity. I could improve stability by replacing my standard deviation with a numerically stable online algorithm, but this would complicate the implementation substantantially (and slow it down). Similarly, implementations using e.g. Kahan(-Babuška-Neumaier) compensations for the SUM and AVG seem to perform modestly better in limited tests, but make the query much more complex. And as long as I do not know how T-SQL implements SUM and AVG (e.g. it might already be using pairwise summation), I cannot guarantee that such modifications always improve accuracy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 402k
  • Answers 402k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'm not sure what exactly you do want to achieve.… May 15, 2026 at 4:51 am
  • Editorial Team
    Editorial Team added an answer Well, I got some code working to attach it: its.Document… May 15, 2026 at 4:51 am
  • Editorial Team
    Editorial Team added an answer java.util.Observable makes many claims on what implementers are expected to… May 15, 2026 at 4:51 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.