- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements
To Continue Learning Please Login
Login with Google