Calculating Linear Regressions or Correlation Coefficients : Variable « Select Clause « SQL / MySQL






Calculating Linear Regressions or Correlation Coefficients

       
mysql>
mysql> CREATE TABLE testscore
    -> (
    ->  subject INT UNSIGNED NOT NULL AUTO_INCREMENT,
    ->  age             INT UNSIGNED NOT NULL,
    ->  sex             ENUM('M','F') NOT NULL,
    ->  score   INT,
    ->  PRIMARY KEY (subject)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> INSERT INTO testscore (age,sex,score)
    ->  VALUES
    ->  (5,'M',5),
    ->  (5,'M',4),
    ->  (5,'F',6),
    ->  (5,'F',7),
    ->  (6,'M',8),
    ->  (6,'M',9),
    ->  (6,'F',4),
    ->  (6,'F',6),
    ->  (7,'M',8),
    ->  (7,'M',6),
    ->  (7,'F',9),
    ->  (7,'F',7),
    ->  (8,'M',9),
    ->  (8,'M',6),
    ->  (8,'F',7),
    ->  (8,'F',10),
    ->  (9,'M',9),
    ->  (9,'M',7),
    ->  (9,'F',10),
    ->  (9,'F',9)
    -> ;
Query OK, 20 rows affected (0.00 sec)
Records: 20  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT subject, age, sex, score FROM testscore ORDER BY subject;
+---------+-----+-----+-------+
| subject | age | sex | score |
+---------+-----+-----+-------+
|       1 |   5 | M   |     5 |
|       2 |   5 | M   |     4 |
|       3 |   5 | F   |     6 |
|       4 |   5 | F   |     7 |
|       5 |   6 | M   |     8 |
|       6 |   6 | M   |     9 |
|       7 |   6 | F   |     4 |
|       8 |   6 | F   |     6 |
|       9 |   7 | M   |     8 |
|      10 |   7 | M   |     6 |
|      11 |   7 | F   |     9 |
|      12 |   7 | F   |     7 |
|      13 |   8 | M   |     9 |
|      14 |   8 | M   |     6 |
|      15 |   8 | F   |     7 |
|      16 |   8 | F   |    10 |
|      17 |   9 | M   |     9 |
|      18 |   9 | M   |     7 |
|      19 |   9 | F   |    10 |
|      20 |   9 | F   |     9 |
+---------+-----+-----+-------+
20 rows in set (0.00 sec)

mysql>
mysql> SELECT
    -> @n := COUNT(score) AS N,
    -> @meanX := AVG(age) AS "X mean",
    -> @sumX := SUM(age) AS "X sum",
    -> @sumXX := SUM(age*age) "X sum of squares",
    -> @meanY := AVG(score) AS "Y mean",
    -> @sumY := SUM(score) AS "Y sum",
    -> @sumYY := SUM(score*score) "Y sum of square",
    -> @sumXY := SUM(age*score) AS "X*Y sum"
    -> FROM testscore\G
*************************** 1. row ***************************
               N: 20
          X mean: 7.000000000
           X sum: 140
X sum of squares: 1020
          Y mean: 7.300000000
           Y sum: 146
 Y sum of square: 1130
         X*Y sum: 1053
1 row in set (0.00 sec)

mysql>
mysql>
mysql> drop table testscore;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
    
    
    
  








Related examples in the same category

1.Variables can be assigned values from arbitrary expressions.
2.Using variable the calculate the new column ID
3.Global System Variables versus System Variables at the Connection Level
4.Using SQL Variables in Queries
5.Returns all global variables and their settings
6.Return the session system variables
7.If you're retrieving the value of a global variable, you must also precede the variable name with the global keyword
8.If you want to retrieve a value for a session variable, rather than a global variable
9.Set the query_cache_limit variable to 1000000 at the global level
10.To set a session-level variable
11.Check the state of the variables with the following command:
12.Ordinary variables indicated by a prefixed @ sign.
13.System and server variables: Such variables contain states or attributes of the MySQL server.
14.Variable Assignment
15.Use of Variables
16.SQL variables hold single values.
17.If the variable has not been used previously, that value is NULL:
18.To set a variable explicitly to a particular value, use a SET statement.
19.Variable names are case sensitive:
20.SQL variables may be used to store the results of intermediate calculations.
21.Use a two-stage approach involving one query that selects the maximum size into a SQL variable, and another th
22.Calculate the overall average and save it in a variable, then compare each driver's average to the saved value
23.To use a SQL variable, store the highest price in it, then use the variable to identify the record containing
24.To find a value and save it in a variable
25.SELECT statement defines two variables
26.User Variables
27.Set a variable specifically.
28.Create the user variable PLAYERNO and initialize it with the value 7.
29.Can the result of a SELECT statement be assigned to a user variable
30.Compare decimal type variables
31.Create and set MySQL variable
32.Using concat to create value for variable
33.Using variable in where clause
34.Assin constant value to a variable
35.Assign the result from a sql to a variable
36.Assign a constant to a variable
37.Assign and create three variables inside one select statement
38.Assign the result of an aggregate function to a variable
39.Using variable to pass value between sql statement
40.Compare to a variable
41.Insert statement with variable
42.Assign value to a variable with :=
43.Using select ... into to assign value to variabls
44.A two-query approach
45.Advancing the rank only when values change
46.Use the differential as follows to produce team standings that include winning percentage and GB values
47.To calculate a standard deviation based on n-1 degrees of freedom instead
48.Assigning Ranks
49.Comparing a Table to Itself