title: How to filter outliers in mySQL?
body: <p>I am trying to filter outliers in a mySQL dataset without success.  I'd like to calculate an average of a column, broken down by date, excluding values that fall outside 2 standard deviations from the mean.</p>
  
  <p>an example table:</p>
  
  <pre><code>+---------------------+----------+
  |      date_time      | duration |
  +---------------------|----------+
  | 2015-01-01 00:01:00 |    1     |
  | 2015-01-01 00:02:00 |    2     |
  | 2015-01-01 00:02:20 |    23    |
  | 2015-01-01 00:03:10 |    3     |
  | 2015-01-02 00:01:00 |    3     |
  | 2015-01-02 00:02:00 |    4     |
  | 2015-01-02 00:02:20 |    52    |
  | 2015-01-02 00:03:10 |    5     |   ...
  </code></pre>
  
  <p>How can I get an average of the duration column excluding the outliers "23" and "52" which are well outside of 2 standard deviations from the mean?</p>
  
  <p>I'd like to get a result like:</p>
  
  <pre><code>+------------+---------+
  |   date     | average |
  +------------+---------|
  | 2015-01-01 |    2    |
  | 2015-02-01 |    4    |
  </code></pre>
  
  <p>I think I need 2 separate queries combined with a join, or a sub query, but I can't figure it out. </p>