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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:20:30+00:00 2026-05-31T12:20:30+00:00

So I have the following SQL code which calculates the pearson correlation between two

  • 0

So I have the following SQL code which calculates the pearson correlation between two users rating:

 select  @u1avg:=avg(user1_rating), 
    @u2avg:=avg(user2_rating), 
    @u1sd:=stddev(user1_rating),
    @u2sd:=stddev(user2_rating)
    from
(select r1.userId as User1_id,r1.rating as User1_rating,
        r2.userId as User2_id,r2.rating as User2_rating

from mydb.ratings r1 join mydb.ratings r2 on r1.itemId = r2.itemid 
where r1.userId=1 and r2.userId=2) sample;


select (1/(count(r1.rating-1)))*sum(((r1.rating-@u1avg)/@u1sd)*((r2.rating-@u2avg)/@u2sd))*(count(r1.rating)/(1+count(r1.rating)))

from mydb.ratings r1 join mydb.ratings r2 on r1.itemId = r2.itemid 
where r1.userId=1 and r2.userId=2;

I would like to turn that into a function, for example corr(A,B)
any help would be useful.

The problem I get is that where it says sample saying not allowed or something like that, however if I remove the sample I get an error saying every table must have alias.

  • 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-31T12:20:31+00:00Added an answer on May 31, 2026 at 12:20 pm

    I think you can do away with the derived table in the first query which will see off that particular error –

    SELECT
        @u1avg:=avg(r1.rating),
        @u2avg:=avg(r2.rating),
        @u1sd:=stddev(r1.rating),
        @u2sd:=stddev(r2.rating)
    FROM mydb.ratings r1
    INNER JOIN mydb.ratings r2
        ON r1.itemId = r2.itemId
    WHERE r1.userId=1
    AND r2.userId=2;
    
    SELECT (1/(COUNT(r1.rating-1)))*SUM(((r1.rating-@u1avg)/@u1sd)*((r2.rating-@u2avg)/@u2sd))*(COUNT(r1.rating)/(1+COUNT(r1.rating)))
    FROM mydb.ratings r1
    INNER JOIN mydb.ratings r2
        ON r1.itemId = r2.itemid
    WHERE r1.userId=1
    AND r2.userId=2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in SQL (2005) which calculates the avarage user logins
I have the following code that fills dataTable1 and dataTable2 with two simple SQL
I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER
I have the following t-sql code which generates an error Declare @table TABLE (
I have the following t-sql code which I have converted to c#. DECLARE @guidRegular
I have the following code which works fine to format text from an SQL
I have the following SQL code: select val.PersonNo, val.event_time, clg.number_dialed from vicidial_agent_log val join
I have the following T-SQL code (in SQL 2000) which functions correctly: INSERT INTO
I've two tables which have following columns: Now I want have a SQL query
I have the following bit of code which runs a SQL statement: int rowsEffected

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.