title: Linear regression, finding slope in MySQL
body: <p>I'm trying to find the slope of a dataset that has DATETIME as the x axis, and a number on the y axis.</p>
  
  <p>I've tried the a number of approaches, and nothing will match the slope of the line when I plug the data into Excel, it's off by multiple orders of magnitude.</p>
  
  <p>This is what I have right now, but it's giving me a slope of -1.13e-13 instead of -0.008</p>
  
  <pre><code>SELECT (SUM((x-xBar)*(y-yBar)))/(SUM((x-xBar))*SUM((x-xBar)))) as slope
  from (select unix_timestamp(date) as x, 
    (select avg(unix_timestamp(date)) from datatable) as xBar, 
     value as y, 
     (select avg(value) from datatable)  as yBar  from datatable) as d;
  </code></pre>
  
  <p>Any help would be greatly appreciated, thanks.</p>
  
  <h3>Update:</h3>
  
  <p>I've also tried</p>
  
  <p><code>SELECT effortId,
  ( COUNT(*)*SUM(unix_timestamp(date)*value) -SUM(unix_timestamp(date))*SUM(value) ) /
  (COUNT(*)*SUM(unix_timestamp(date)^2)-SUM(unix_timestamp(date))^2) AS Slope
  FROM datatable;</code></p>
  
  <p>and get a completely different answer (-0.0019), is this more accurate? Anyone know?</p>