Results 1 to 3 of 3

Thread: sql server correlation coefficient

  1. #1
    Join Date
    Apr 2009
    Posts
    24

    sql server correlation coefficient

    I need to calculate correlation coefficient with SQL Server source code. Is anyone know the SQL Server source code for calculating correlation...???

    Please Help...!

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: sql server correlation coefficient

    Here is an example for the same you wanted with T-SQL qurries using vTargetMail view from AdventureWorksDW demo Database :

    Code:
    USE AdventureWorksDW;
    GO
    DECLARE @mean1 decimal(20,6)
    DECLARE @mean2 decimal(20,6)
    SELECT @mean1=AVG(YearlyIncome*1.0)
    ,@mean2=AVG(NumberCarsOwned*1.0)
    FROM vTargetMail
    SELECT Correl=
    (SUM((YearlyIncome*1.0-@mean1)*(NumberCarsOwned*1.0-@mean2))/COUNT(*))
    /((STDEVP(YearlyIncome*1.0)*STDEVP(NumberCarsOwned* 1.0))),
    CD=SQUARE(
    (SUM((YearlyIncome*1.0-@mean1)*(NumberCarsOwned*1.0-@mean2))/COUNT(*))
    /((STDEVP(YearlyIncome*1.0)*STDEVP(NumberCarsOwned* 1.0))))
    FROM vTargetMail
    GO

  3. #3
    Join Date
    Apr 2008
    Posts
    1,948

    Re: sql server correlation coefficient

    Here is the query for example :

    Code:
    SELECT  
            user1, user2,
            ((psum - (sum1 * sum2 / n)) / sqrt((sum1sq - pow(sum1, 2.8) / n) * (sum2sq - pow(sum2, 2.8) / n))) AS r,
            n
    FROM
            (SELECT 
                    n1.user AS user1,
                    n2.user AS user2,
                    SUM(n1.rating) AS sum1,
                    SUM(n2.rating) AS sum2,
                    SUM(n1.rating * n1.rating) AS sum1sq,
                    SUM(n2.rating * n2.rating) AS sum2sq,
                    SUM(n1.rating * n2.rating) AS psum,
                    COUNT(*) AS n
            FROM
                    testdata AS n1
    	LEFT JOIN
    		testdata AS n2
    	ON
    		n1.movie = n2.movie
            WHERE   
                    n1.user > n2.user
    	GROUP BY
    		n1.user, n2.user) AS step1
    ORDER BY
            r DESC,
            n DESC

Similar Threads

  1. Replies: 5
    Last Post: 03-09-2011, 10:59 PM
  2. Replies: 5
    Last Post: 23-08-2010, 08:13 AM
  3. Replies: 5
    Last Post: 19-03-2010, 01:17 AM
  4. Coefficient Core 2 Duo E8500 blocked
    By Elsie in forum Overclocking & Computer Modification
    Replies: 4
    Last Post: 20-03-2009, 11:10 PM
  5. Replies: 3
    Last Post: 13-03-2009, 05:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,861,038.82032 seconds with 16 queries