如何使用1 mySQL查询返回所有结果的数字箱图数据?

Wou*_*elo 7 mysql math percentile

[tbl_votes]
- id <!-- unique id of the vote) -->
- item_id <!-- vote belongs to item <id> -->
- vote <!-- number 1-10 -->
Run Code Online (Sandbox Code Playgroud)

当然,我们可以通过获得:

  • smallest observation(所以)
  • lower quartile(LQ)
  • median(我)
  • upper quartile(UQ)
  • largest observation(lo)

..一个一个地使用多个查询,但我想知道是否可以使用单个查询完成.

在Oracle中我可以使用COUNT OVERRATIO_TO_REPORT,但mySQL不支持.

对于那些不知道盒子图是什么的人:http://en.wikipedia.org/wiki/Box_plot

任何帮助,将不胜感激.

new*_*ver 0

e256下面是计算组内值范围四分位数的示例e32,在这种情况下,(e32, e256) 上的索引是必须的:

SELECT
  @group:=IF(e32=@group, e32, GREATEST(@index:=-1, e32)) as e32_,
  MIN(e256) as so,
  MAX(IF(lq_i=(@index:=@index+1), e256, NULL)) as lq,
  MAX(IF(me_i=@index, e256, NULL)) as me,
  MAX(IF(uq_i=@index, e256, NULL)) as uq,
  MAX(e256) as lo
FROM (SELECT @index:=NULL, @group:=NULL) as init, test t
JOIN (
  SELECT e32,
    COUNT(*) as cnt,
    (COUNT(*) div 4) as lq_i,    -- lq value index within the group
    (COUNT(*) div 2) as me_i,    -- me value index within the group
    (COUNT(*) * 3 div 4) as uq_i -- uq value index within the group
  FROM test
  GROUP BY e32
) as cnts
USING (e32)
GROUP BY e32;
Run Code Online (Sandbox Code Playgroud)

如果不需要分组,查询会稍微简单一些。

PStest是我的随机值游乐场表,其中e32是 Python 等的结果int(random.expovariate(1.0) * 32)