mysql - 如何在没有组的 MIN() 和 MAX() 的情况下获取项目组的 STDDEV()

标签 mysql sql aggregate-functions standard-deviation

我有一张 table

| id | user | bottle |  count |
| 1  | foo  | beer   |    2   |
| 2  | bar  | beer   |    5   |
| 3  | som1 | beer   |    6   |
| 4  | som2 | beer   |    4   |
| 5  | som1 | wine   |    1   |

等等

如果没有 countMIN()MAX() 值,我如何获得 STDDEV() > 每组(啤酒、 Wine 等)?
即我如何获得

| bottle |      stddev     |

|  beer  |stddev of 2 and 4|

?

另外,如何获取上述查询返回的总行数?

到目前为止我已经尝试过了

SELECT STD(count) as stddev, bottle
FROM drinks WHERE count < (
    SELECT MAX(count) FROM drinks)
              AND count > (
    SELECT MIN(count) FROM drinks)
GROUP BY bottle;

但这适用于整个表的 MAX() 而不仅仅是 分组依据

最佳答案

使用聚合和连接引入极值,然后过滤掉它们并计算标准差:

select bottle, STDEV(d.count)
from drd.inks d join
     (select bottle, MIN(count) as minc, MAX(count) as maxc
      from drinks
      group by bottle
     ) dsum
     on d.bottle = dsum.bottle and
        d.count not in (dsum.minc, dsum.maxc)
group by d.bottle

关于mysql - 如何在没有组的 MIN() 和 MAX() 的情况下获取项目组的 STDDEV(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17408436/

相关文章:

php - 配置文件 View 更新

mysql - 按日期+其他字段对多个字段求和和分组

mysql - 结合2个Mysql更新语句(相同列,不同值,不同条件)

mysql - 如何合并两个具有不同 WHERE 和特殊条件的选择

SQL Server 索引包含的列

sql - 插入触发器上不存在 Postgres 错误关系 "new"

php - 无法使用 codeigniter 列出另一个表数据

MySQL - SUM/COUNT 来自不同表的不同列

mysql - SQL 仅选择列上具有最大值的行

mysql - 如何限制/剔除一些符合条件的记录? (正确的语法)