
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Is it possible to calculate a correlation in a MySQL query?
Yes, it is possible to calculate a correlation in a query. To understand the correlation in a query, you need to first create a table. The query to create a table is as follows
mysql> create table correlationDemo - > ( - > value float not null, - > value2 float not null - > ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table with the help of insert command. The query is as follows to insert records in the table
mysql> insert into correlationDemo values(1,10); Query OK, 1 row affected (0.19 sec) mysql> insert into correlationDemo values(2,4); Query OK, 1 row affected (0.16 sec) mysql> insert into correlationDemo values(3,5); Query OK, 1 row affected (0.14 sec) mysql> insert into correlationDemo values(6,17); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from correlationDemo;
The following is the output
+-------+--------+ | value | value2 | +-------+--------+ | 1 | 10 | | 2 | 4 | | 3 | 5 | | 6 | 17 | +-------+--------+ 4 rows in set (0.03 sec)
Now here is the simple correlation in a query
mysql> select @firstValue:=avg(value), - > @secondValue:=avg(value2), - > @division:=(stddev_samp(value) * stddev_samp(value2)) from correlationDemo;
The following is the output
+-------------------------+---------------------------+-------------------------------------------------------+ | @firstValue:=avg(value) | @secondValue:=avg(value2) | @division:=(stddev_samp(value) *stddev_samp(value2)) | +-------------------------+---------------------------+-------------------------------------------------------+ | 3 | 9 | 12.84090685617215 | +-------------------------+---------------------------+-------------------------------------------------------+ 1 row in set (0.00 sec)
Here is the calculation of the above correlation query
mysql> select sum( ( value - @firstValue ) * (value2 - @secondValue) ) / ((count(value) -1) * @division) from - > correlationDemo;
The following is the output
+--------------------------------------------------------------------------------------------+ | sum( ( value - @firstValue ) * (value2 - @secondValue) ) / ((count(value) -1) * @division) | +--------------------------------------------------------------------------------------------+ | 0.7008850777290727 | +--------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
- Related Articles
- Is it possible to cast in a MongoDB Query?
- Is it possible to use UPDATE query with LIMIT in MySQL?
- Is it possible to make a case-insensitive query in MongoDB?
- Is it possible to have a function-based index in MySQL?
- Is it possible to return a list of specific values from a query in MongoDB?
- Is it possible to make an insert or an update in the same MySQL query?
- Is it possible to return a list of specific values from a MongoDB query?
- A single MySQL select query on two tables is possible?
- In MySQL, how it can be possible to specify a sort order using a column that is not retrieved by the query?
- Calculate average of numbers in a column MySQL query?
- MySQL query to calculate the average of values in a row?
- Is it possible to delete everything after a 'space' in a MySQL field?
- How is it possible for a MySQL trigger to execute multiple statements?
- Insert the results of a MySQL select? Is it possible?
- How can it be possible to invert a string in MySQL?

Advertisements