{"id":70534,"date":"2017-04-06T15:36:50","date_gmt":"2017-04-06T15:36:50","guid":{"rendered":"https:\/\/www.simple-talk.com\/?p=70534"},"modified":"2021-09-29T16:21:16","modified_gmt":"2021-09-29T16:21:16","slug":"statistics-sql-pearsons-correlation","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/statistics-sql-pearsons-correlation\/","title":{"rendered":"Statistics in SQL: Pearson&#8217;s Correlation"},"content":{"rendered":"<h4>The series so far:<\/h4>\n<ol>\n<li><a href=\"https:\/\/www.simple-talk.com\/blogs\/statistics-sql-pearsons-correlation\/\">Statistics in SQL: Pearson\u2019s Correlation<\/a><\/li>\n<li><a href=\"https:\/\/www.simple-talk.com\/blogs\/statistics-sql-kendalls-tau-rank-correlation\/\">Statistics in SQL: Kendall\u2019s Tau Rank Correlation<\/a><\/li>\n<li><a href=\"https:\/\/www.simple-talk.com\/blogs\/statistics-sql-simple-linear-regressions\/\">Statistics in SQL: Simple Linear Regressions<\/a><\/li>\n<li><a href=\"https:\/\/www.simple-talk.com\/blogs\/statistics-sql-kruskal-wallis-test\/\">Statistics in SQL: The Kruskal\u2013Wallis Test<\/a><\/li>\n<li><a href=\"https:\/\/www.simple-talk.com\/blogs\/statistics-sql-mann-whitney-u-test\/\">Statistics in SQL:  The Mann\u2013Whitney U Test<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/blogs\/statistics-sql-students-t-test\/\">Statistics in SQL:  Student's T Test<\/a><\/li>\n<\/ol>\n\n<p>When you have two variables, one of the first questions you\u2019d probably like to ask is, \u2018how closely are they related?\u2019 These variables could be attributes whose values come from measurements, scores or other types of quantity. Are you eager to puzzle out what factors are related the number of purchases in your website per day, for example? You want some sort of measure of how the various attributes relate together in their values. You have two choices, depending on whether your data is \u2018parametric\u2019 or not. A variable is \u2018parametric\u2019 if it is representative of a normally-distributed (bell-curve) population. If it isn\u2019t, then you\u2019d want to use something like Kendall\u2019s rank correlation or<a style=\"text-decoration: none;\" href=\"https:\/\/en.wikipedia.org\/wiki\/Spearman%27s_rank_correlation_coefficient\"> <span style=\"color: #1155cc; text-decoration: underline; vertical-align: baseline;\">Spearman\u2019s rank correlation<\/span><\/a> coefficient, but if your measurements are parametric, then Pearson\u2019s Product-moment Correlation Coefficient is best.<\/p>\n<p>Karl Pearson was an odd cove who had some very weird ideas about eugenics. He and Francis Galton were out to distinguish clear differences of racial types, using scientific measurement. He was a gifted mathematician whose work influenced Einstein, and which founded the science of Psychology, but his delusions about eugenics were to have ghastly consequences in the twentieth century. \u00a0\u00a0<\/p>\n<p>Pearson\u2019s Product-moment Correlation Coefficient gives a measurement from -1 for a perfect negative correlation (as one variable goes up, the other goes down) to 1 for a perfect correlation (as one variable goes up, the other goes up). a correlation of 0 means that there is no relationship between the two. Pearson&#8217;s correlation coefficient is calculated as the covariance of the two variables divided by the product of their standard deviations.<\/p>\n<p>You can go further to calculate the probability that this correlation occurred by chance. This only gives a meaningful figure if both variables are normally distributed.<\/p>\n<p>Using SQL, here is how one might calculate Pearson&#8217;s correlation coefficient when applied to a sample. It is usually represented by the letter r, and is the sample correlation coefficient. We will use the formula<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-70535\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/04\/pearsonsCorrelation.jpg\" alt=\"pearsonsCorrelation\" width=\"316\" height=\"133\" \/><\/p>\n<p>\u2026which is easier to do in SQL, though not entirely easy on the eye for a non-statistician. \u00a0My expression in the following SQL merely translates this into something any SQL programmer will greet with a grin of recognition. That big squiggly E means \u2018sum()\u2019 in SQL, and N is count(*). Later, I&#8217;ll show an easier way of calculating it, using the built-in aggregation functions (StDev() and StDevP()) designed for this sort of job.<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px; font: 400 11px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\">DECLARE<\/span> <span style=\"color: teal;\">@OurData<\/span> <span style=\"color: blue;\">TABLE<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\u00a0<span style=\"color: teal;\">x<\/span> <span style=\"color: blue;\">NUMERIC<\/span><span style=\"color: grey;\">(<\/span>18<span style=\"color: grey;\">,<\/span> 6<span style=\"color: grey;\">)<\/span> <span style=\"color: grey;\">NOT<\/span> <span style=\"color: grey;\">NULL,<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\u00a0<span style=\"color: teal;\">y<\/span> <span style=\"color: blue;\">NUMERIC<\/span><span style=\"color: grey;\">(<\/span>18<span style=\"color: grey;\">,<\/span> 6<span style=\"color: grey;\">)<\/span> <span style=\"color: grey;\">NOT<\/span> <span style=\"color: grey;\">NULL<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\u00a0<span style=\"color: grey;\">);<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\u00a0<\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\">INSERT<\/span> <span style=\"color: blue;\">INTO<\/span> <span style=\"color: teal;\">@OurData<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span><span style=\"color: teal;\">x<\/span><span style=\"color: grey;\">,<\/span> <span style=\"color: teal;\">y<\/span><span style=\"color: grey;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\">VALUES<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>10<span style=\"color: grey;\">,<\/span> 7<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>9<span style=\"color: grey;\">,<\/span> 13<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>13<span style=\"color: grey;\">,<\/span> 11<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>9<span style=\"color: grey;\">,<\/span> 8<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>12<span style=\"color: grey;\">,<\/span> 13<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>9<span style=\"color: grey;\">,<\/span> 11<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>10<span style=\"color: grey;\">,<\/span> 11<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>10<span style=\"color: grey;\">,<\/span> 12<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>9<span style=\"color: grey;\">,<\/span> 8<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>11<span style=\"color: grey;\">,<\/span> 9<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>13<span style=\"color: grey;\">,<\/span> 13<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>10<span style=\"color: grey;\">,<\/span> 11<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>14<span style=\"color: grey;\">,<\/span> 15<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>11<span style=\"color: grey;\">,<\/span> 13<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>8<span style=\"color: grey;\">,<\/span> 8<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>13<span style=\"color: grey;\">,<\/span> 13<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>13<span style=\"color: grey;\">,<\/span> 11<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>12<span style=\"color: grey;\">,<\/span> 10<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>10<span style=\"color: grey;\">,<\/span> 12<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>12<span style=\"color: grey;\">,<\/span> 7<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>10<span style=\"color: grey;\">,<\/span> 12<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>10<span style=\"color: grey;\">,<\/span> 12<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>7<span style=\"color: grey;\">,<\/span> 9<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>10<span style=\"color: grey;\">,<\/span> 11<span style=\"color: grey;\">),<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\"> \u00a0<\/span><span style=\"color: grey;\">(<\/span>11<span style=\"color: grey;\">,<\/span> 8<span style=\"color: grey;\">);<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\u00a0<\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\u00a0<\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: blue;\">SELECT <\/span><span style=\"color: grey;\">((<\/span><span style=\"color: magenta;\">SUM<\/span><span style=\"color: grey;\">(<\/span><span style=\"color: teal;\">x<\/span> <span style=\"color: grey;\">*<\/span> <span style=\"color: teal;\">y<\/span><span style=\"color: grey;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: grey;\">&#8211;<\/span> <span style=\"color: grey;\">(<\/span><span style=\"color: magenta;\">SUM<\/span><span style=\"color: grey;\">(<\/span><span style=\"color: teal;\">x<\/span><span style=\"color: grey;\">)<\/span> <span style=\"color: grey;\">*<\/span> <span style=\"color: magenta;\">SUM<\/span><span style=\"color: grey;\">(<\/span><span style=\"color: teal;\">y<\/span><span style=\"color: grey;\">))<\/span> <span style=\"color: grey;\">\/<\/span> <span style=\"color: magenta;\">COUNT<\/span><span style=\"color: grey;\">(*)<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\u00a0\u00a0\u00a0 <span style=\"color: grey;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\u00a0\u00a0 <span style=\"color: grey;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\u00a0\u00a0 <span style=\"color: grey;\">\/<\/span> <span style=\"color: grey;\">(<\/span><span style=\"color: magenta;\">SQRT<\/span><span style=\"color: grey;\">(<\/span><span style=\"color: magenta;\">SUM<\/span><span style=\"color: grey;\">(<\/span><span style=\"color: teal;\">x<\/span> <span style=\"color: grey;\">*<\/span> <span style=\"color: teal;\">x<\/span><span style=\"color: grey;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: grey;\">&#8211;<\/span> <span style=\"color: grey;\">(<\/span><span style=\"color: magenta;\">SUM<\/span><span style=\"color: grey;\">(<\/span><span style=\"color: teal;\">x<\/span><span style=\"color: grey;\">)<\/span> <span style=\"color: grey;\">*<\/span> <span style=\"color: magenta;\">SUM<\/span> <span style=\"color: grey;\">(<\/span><span style=\"color: teal;\">x<\/span><span style=\"color: grey;\">))<\/span> <span style=\"color: grey;\">\/<\/span> <span style=\"color: magenta;\">COUNT<\/span><span style=\"color: grey;\">(*)<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: grey;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: grey;\">*<\/span> <span style=\"color: magenta;\">SQRT<\/span><span style=\"color: grey;\">(<\/span><span style=\"color: magenta;\">SUM<\/span><span style=\"color: grey;\">(<\/span><span style=\"color: teal;\">y<\/span> <span style=\"color: grey;\">*<\/span> <span style=\"color: teal;\">y<\/span><span style=\"color: grey;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: grey;\">&#8211;<\/span> <span style=\"color: grey;\">(<\/span><span style=\"color: magenta;\">SUM<\/span><span style=\"color: grey;\">(<\/span><span style=\"color: teal;\">y<\/span><span style=\"color: grey;\">)<\/span> <span style=\"color: grey;\">*<\/span> <span style=\"color: magenta;\">SUM<\/span><span style=\"color: grey;\">(<\/span><span style=\"color: teal;\">y<\/span><span style=\"color: grey;\">))<\/span> <span style=\"color: grey;\">\/<\/span> <span style=\"color: magenta;\">COUNT<\/span><span style=\"color: grey;\">(*)<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: grey;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\u00a0\u00a0\u00a0\u00a0 <span style=\"color: grey;\">)<\/span> <span style=\"color: blue;\">AS<\/span> <span style=\"color: red;\">&#8216;Pearsons r&#8217;<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\u00a0<span style=\"color: blue;\">FROM<\/span> <span style=\"color: teal;\">@OurData<\/span><span style=\"color: grey;\">;<\/span><\/p>\n<\/div>\n<p>&#8230; giving &#8230;<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px; font: 400 11px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"font-family: Arial; font-size: 9pt; vertical-align: baseline;\">(25 row(s) affected)<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"font-family: Arial; font-size: 9pt; vertical-align: baseline;\">Pearsons r<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"font-family: Arial; font-size: 9pt; vertical-align: baseline;\">0.428590216844083<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\u00a0<\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"font-family: Arial; font-size: 9pt; vertical-align: baseline;\">(1 row(s) affected)<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\u00a0<\/p>\n<\/div>\n<p>SQL Server has a built-in function for calculating the population variance \u00a0that makes it simpler to calculate<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px; font: 400 11px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9; color: green;\">\/*or more shortly, using the built-in StDevP function giving the<\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"font-family: Consolas; color: green;\"> \u00a0statistical standard deviation for the population for all values *\/<\/span><\/p>\n<p><span style=\"font-family: Consolas;\"> \u00a0<\/span><span style=\"font-size: 9pt; font-family: Consolas;\"><span style=\"color: blue;\">SELECT <\/span> <span style=\"color: gray;\">(<\/span><span style=\"color: fuchsia;\">Avg<\/span><span style=\"color: gray;\">(<\/span><span style=\"color: teal;\">x<\/span> <span style=\"color: gray;\">*<\/span> <span style=\"color: teal;\">y<\/span><span style=\"color: gray;\">)<\/span> <span style=\"color: gray;\">&#8211;<\/span> <span style=\"color: gray;\">(<\/span><span style=\"color: fuchsia;\">Avg<\/span><span style=\"color: gray;\">(<\/span><span style=\"color: teal;\">x<\/span><span style=\"color: gray;\">)<\/span> <span style=\"color: gray;\">*<\/span> <span style=\"color: fuchsia;\">Avg<\/span><span style=\"color: gray;\">(<\/span><span style=\"color: teal;\">y<\/span><span style=\"color: gray;\">)))<\/span> <span style=\"color: gray;\">\/<\/span> <span style=\"color: gray;\">(<\/span><span style=\"color: fuchsia;\">StDevP<\/span><span style=\"color: gray;\">(<\/span><span style=\"color: teal;\">x<\/span><span style=\"color: gray;\">)<\/span> <span style=\"color: gray;\">*<\/span> <span style=\"color: fuchsia;\">StDevP<\/span><span style=\"color: gray;\">(<\/span><span style=\"color: teal;\">y<\/span><span style=\"color: gray;\">))<\/span> <span style=\"color: blue;\">AS<\/span> <span style=\"color: red;\">&#8216;Pearsons r&#8217;<\/span><\/span><\/p>\n<p><span style=\"font-size: 9pt; font-family: Consolas;\"> \u00a0<span style=\"color: blue;\">FROM<\/span> <span style=\"color: teal;\">@ourData<\/span><\/span><\/p>\n<\/div>\n<p>&#8230; giving &#8230;<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px; font: 400 11px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"font-family: Arial; font-size: 9pt; vertical-align: baseline;\">Pearsons r<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"font-family: Arial; font-size: 9pt; vertical-align: baseline;\">0.428590216844083<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\u00a0<\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"font-family: Arial; font-size: 9pt; vertical-align: baseline;\">(1 row(s) affected)<\/span><\/p>\n<p style=\"margin: 0; font: 400 12px Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\u00a0<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>You\u2019d probably just want it to a couple of decimal places.<\/p>\n<div class=\"drop\">\u00a0For a good introduction to using correlation in SQL see <a href=\"https:\/\/www.simple-talk.com\/sql\/t-sql-programming\/causation-correlation-and-crackpots\/\">Causation, Correlation and Crackpots,<\/a> by Joe Celko.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Some people will assure you that you can&#8217;t do any serious statistical calculations in SQL.  In the first of a series of articles, Phil factor aims to prove them wrong by explaining how easy it is to calculate Pearson&#8217;s Product Moment Correlation.&hellip;<\/p>\n","protected":false},"author":154613,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143528,2,143531],"tags":[43637],"coauthors":[6813],"class_list":["post-70534","post","type-post","status-publish","format-standard","hentry","category-bi-sql-server","category-blogs","category-t-sql-programming-sql-server","tag-statisticsinsql"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/70534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/154613"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=70534"}],"version-history":[{"count":13,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/70534\/revisions"}],"predecessor-version":[{"id":71973,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/70534\/revisions\/71973"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=70534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=70534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=70534"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=70534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}