7

我有以下运行良好的 MySQL 查询:

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) >= 2012 and 
    name like "%the_name%" and 
    QNTYDelivered > 0 and 
    QNTY_Sell > 0 
group by name 
order by name;

现在我还希望添加一个结果列,它为我提供每行数据的 MEDIAN。在SELECT这个完美的世界中,这看起来像这样:

median((QNTY_Sell/QNTYDelivered)*1000) as `MEDIAN Thousand Price`

在 Google 中搜索 MySQL 中值函数让我得到了这个答案,如果您对整个表的数据集的中值感兴趣,这似乎没问题: Simple way to calculate median with MySQL

这里的区别是我按name列对表中的数据进行分组,并希望获得按此列分组的每一行数据的中位数。

有谁知道允许我这样做的绝妙技巧?

谢谢!

4

2 回答 2

3

即使没有内置的中值函数,您也可以在 MySQL 中使用 GROUP BY 计算中值。

考虑下表:

Acrington   200.00
Acrington   200.00
Acrington   300.00
Acrington   400.00
Bulingdon   200.00
Bulingdon   300.00
Bulingdon   400.00
Bulingdon   500.00
Cardington  100.00
Cardington  149.00
Cardington  151.00
Cardington  300.00
Cardington  300.00

对于每一行,您可以计算较少的相似项目的数量。您还可以计算有多少值小于或等于:

name        v       <   <=
Acrington   200.00  0   2
Acrington   200.00  0   2
Acrington   300.00  2   3
Acrington   400.00  3   4
Bulingdon   200.00  0   1
Bulingdon   300.00  1   2
Bulingdon   400.00  2   3
Bulingdon   500.00  3   4
Cardington  100.00  0   1
Cardington  149.00  1   2
Cardington  151.00  2   3
Cardington  300.00  3   5
Cardington  300.00  3   5

有查询

SELECT name,v, (SELECT COUNT(1) FROM sale WHERE v<o.v AND name=o.name) as ls
             , (SELECT COUNT(1) FROM sale WHERE v<=o.v AND name=o.name) as lse
  FROM sale o

当小于或等于计数是项目数的一半时,将出现中值

  • 阿克灵顿有 4 个项目。其中一半是 2,它在 0..2 范围内(对应于 200.00),也在 2..3 范围内(对应于 300.00)

  • Bullingdon也有 4 个项目。2 在 1..2(值 300.00)和 2..3(值 400.00)范围内

  • 卡丁顿有 5 个项目。值 2.5 介于 2 和 3 之间,对应于 Cardington 151。

中值是由以下函数返回的最小值和最大值的平均值:

SELECT cs.name,v
   FROM
   (SELECT name,v, (SELECT COUNT(1) FROM sale WHERE v<o.v AND name=o.name) as ls
                 , (SELECT COUNT(1) FROM sale WHERE v<=o.v AND name=o.name) as lse
      FROM sale o) cs JOIN
   (SELECT name,COUNT(1)*.5 as cn
      FROM sale
      GROUP BY name) cc ON cs.name=cc.name
 WHERE cn between ls and lse

这使:

Acrington   200.00
Acrington   200.00
Acrington   300.00
Bulingdon   300.00
Bulingdon   400.00
Cardington  151.00

最后我们可以得到中位数:

SELECT name,(MAX(v)+MIN(v))/2 FROM
(SELECT cs.name,v
   FROM
   (SELECT name,v, (SELECT COUNT(1) FROM sale WHERE v<o.v AND name=o.name) as ls
                 , (SELECT COUNT(1) FROM sale WHERE v<=o.v AND name=o.name) as lse
      FROM sale o) cs JOIN
   (SELECT name,COUNT(1)*.5 as cn
      FROM sale
     GROUP BY name) cc ON cs.name=cc.name
 WHERE cn between ls and lse
 ) AS medians
GROUP BY name

给予

Acrington   250.000000
Bulingdon   350.000000
Cardington  151.000000
于 2014-05-06T02:07:50.347 回答
3

我发现这样做的唯一方法是通过字符串操作:创建所有值
GROUP_CONCAT列表,然后缩进取SUBSTRING_INDEX中值

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`
  , CASE (count(*) % 2)
    WHEN 1 THEN SUBSTRING_INDEX(
      SUBSTRING_INDEX(
        group_concat((QNTY_Sell/QNTYDelivered)*1000 
                      ORDER BY (QNTY_Sell/QNTYDelivered)*1000 SEPARATOR ',')
      , ',', (count(*) + 1) / 2)
    , ',', -1)
    ELSE (SUBSTRING_INDEX(
      SUBSTRING_INDEX(
        group_concat((QNTY_Sell/QNTYDelivered)*1000 
                      ORDER BY (QNTY_Sell/QNTYDelivered)*1000 SEPARATOR ',')
      , ',', count(*) / 2)
    , ',', -1)
  + SUBSTRING_INDEX(
      SUBSTRING_INDEX(
        group_concat((QNTY_Sell/QNTYDelivered)*1000 
                      ORDER BY (QNTY_Sell/QNTYDelivered)*1000 SEPARATOR ',')
      , ',', (count(*) + 1) / 2)
    , ',', -1)) / 2
    END median
FROM
    sales
WHERE
    year(date) >= 2012 AND
    name LIKE "%art.%" AND
    QNTYDelivered > 0 AND
    QNTY_Sell > 0
GROUP BY name
ORDER BY name;  

需要 CASE 来检查我们是否有一个具有奇数个值的中值,或两个具有偶数个值的中值,在第二种情况下,中值是所建立的两个值的平均值。

SQLFiddle

于 2014-04-08T10:32:44.983 回答