title: Using MEDIAN along side MAX, MIN, and AVG functions in MySQL
body: <p>I have the following MySQL query which is working perfectly:</p>
  
  <pre><code>select 
      count(*) as `# of Data points`, 
      name, 
      max((QNTY_Sell/QNTYDelivered)*1000) as `MAX Thousand Price`,
      min((QNTY_Sell/QNTYDelivered)*1000) as `MIN Thousand Price`,
      avg((QNTY_Sell/QNTYDelivered)*1000) as `MEAN Thousand Price` 
  from 
      table_name 
  where 
      year(date) &gt;= 2012 and 
      name like "%the_name%" and 
      QNTYDelivered &gt; 0 and 
      QNTY_Sell &gt; 0 
  group by name 
  order by name;
  </code></pre>
  
  <p>Now I wish to also add a result column that gives me the MEDIAN of the data for each line.  Under <code>SELECT</code> this would look like this in a perfect world:</p>
  
  <pre><code>median((QNTY_Sell/QNTYDelivered)*1000) as `MEDIAN Thousand Price`
  </code></pre>
  
  <p>Searching Google for a MySQL median function brought me to this answer, which seems ok if you are interested in the median of a data set for an entire table:  <a href="https://stackoverflow.com/questions/1291152/simple-way-to-calculate-median-with-mysql">Simple way to calculate median with MySQL</a></p>
  
  <p>The difference here is that I am grouping the data in my table by the <code>name</code> column, and want to get the median for each line of the data grouped by this column.</p>
  
  <p>Does anyone know a nifty trick to allow me to do this?</p>
  
  <p>Thanks!</p>