Are there any Linear Regression Function in SQL Server?

前端 未结 8 1589
遇见更好的自我
遇见更好的自我 2020-12-12 17:19

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

8条回答
  •  感情败类
    2020-12-12 18:10

    This is an alternate method, based off a blog post on Linear Regression in T-SQL, which uses the following equations:

    enter image description here

    The SQL suggestion in the blog uses cursors though. Here's a prettified version of a forum answer that I used:

    table
    -----
    X (numeric)
    Y (numeric)
    
    /**
     * m = (nSxy - SxSy) / (nSxx - SxSx)
     * b = Ay - (Ax * m)
     * N.B. S = Sum, A = Mean
     */
    DECLARE @n INT
    SELECT @n = COUNT(*) FROM table
    SELECT (@n * SUM(X*Y) - SUM(X) * SUM(Y)) / (@n * SUM(X*X) - SUM(X) * SUM(X)) AS M,
           AVG(Y) - AVG(X) *
           (@n * SUM(X*Y) - SUM(X) * SUM(Y)) / (@n * SUM(X*X) - SUM(X) * SUM(X)) AS B
    FROM table
    

提交回复
热议问题